这两者的写法有什么区别?那一种更好呢?
import java.io.File;
import java.io.RandomAccessFile;public class Fen { String fileName;
int size; // size 分割成单独文件的大小
Fen(String fileName, int size) {
this.fileName = fileName;
this.size = size * 1024;
} public void cut() throws Exception { int maxx = 0;
File inFile = new File(fileName);
int count = 0; // 记录汉字数,一个汉字计数增加2
int fileLength = (int) inFile.length(); // 取得文件的大小
int value; // 取得要分割的个数 // 打开要分割的文件 //并将内容读入到 inn中
RandomAccessFile inn = new RandomAccessFile(inFile, "r"); value = fileLength / size;// 原本相除是小数的 但这个都是int型
// 所以如果只输出value个文件的话 后面的还剩的一些不足一个文件的data就丢失了 //问题在这里 和下面的写法有什么不同?
RandomAccessFile out;//像这样把变量的声明放在外面和放在循环里面有什么不同?
File outFile;
int j = 0;
// 根据要分割的数目输出文件
for (; j < value; j++) {
outFile = new File(inFile.getName() + j + ".txt");
out= new RandomAccessFile(outFile, "rw");
int temp = 0;
maxx += size; int i = 0;
while (i < maxx && (temp = inn.read()) != -1) {
if (temp > 127) { // 大于127表示读了半个汉字
count = ++count % 2; // 模2防止count溢出
}
out.write(temp);
i++;
}
if (count == 1 && (temp = inn.read()) != -1) { // count若为1表示最后读取了半个汉字
out.write(temp);
maxx++;
}
count = 0;
out.close();
} // 输出剩余的内容
outFile = new File(inFile.getName() + j + ".txt");
out = new RandomAccessFile(outFile, "rw");
int temp = 0;
while ((temp = inn.read()) != -1) {
out.write(temp);
}
out.close();
inn.close();
} public static void main(String[] args) throws Exception {
Fen cutt = new Fen("test.txt", 7);
cutt.cut();
}
}
写法2====================================================
import java.io.File;
import java.io.RandomAccessFile;public class Fen { String fileName;
int size; // size 分割成单独文件的大小
Fen(String fileName, int size) {
this.fileName = fileName;
this.size = size * 1024;
} public void cut() throws Exception { int maxx = 0;
File inFile = new File(fileName);
int count = 0; // 记录汉字数,一个汉字计数增加2
int fileLength = (int) inFile.length(); // 取得文件的大小
int value; // 取得要分割的个数 // 打开要分割的文件 //并将内容读入到 inn中
RandomAccessFile inn = new RandomAccessFile(inFile, "r"); value = fileLength / size;// 原本相除是小数的 但这个都是int型
// 所以如果只输出value个文件的话 后面的还剩的一些不足一个文件的data就丢失了
int j = 0;
// 根据要分割的数目输出文件
for (; j < value; j++) {
File outFile = new File(inFile.getName() + j + ".txt");
RandomAccessFile out= new RandomAccessFile(outFile, "rw");
int temp = 0;
maxx += size; int i = 0;
while (i < maxx && (temp = inn.read()) != -1) {
if (temp > 127) { // 大于127表示读了半个汉字
count = ++count % 2; // 模2防止count溢出
}
out.write(temp);
i++;
}
if (count == 1 && (temp = inn.read()) != -1) { // count若为1表示最后读取了半个汉字
out.write(temp);
maxx++;
}
count = 0;
out.close();
} // 输出剩余的内容
File outFile = new File(inFile.getName() + j + ".txt");
RandomAccessFile out2 = new RandomAccessFile(outFile, "rw");
int temp = 0;
while ((temp = inn.read()) != -1) {
out2.write(temp);
}
out2.close(); inn.close();
} public static void main(String[] args) throws Exception {
Fen cutt = new Fen("test.txt", 7);
cutt.cut();
}
}
import java.io.File;
import java.io.RandomAccessFile;public class Fen { String fileName;
int size; // size 分割成单独文件的大小
Fen(String fileName, int size) {
this.fileName = fileName;
this.size = size * 1024;
} public void cut() throws Exception { int maxx = 0;
File inFile = new File(fileName);
int count = 0; // 记录汉字数,一个汉字计数增加2
int fileLength = (int) inFile.length(); // 取得文件的大小
int value; // 取得要分割的个数 // 打开要分割的文件 //并将内容读入到 inn中
RandomAccessFile inn = new RandomAccessFile(inFile, "r"); value = fileLength / size;// 原本相除是小数的 但这个都是int型
// 所以如果只输出value个文件的话 后面的还剩的一些不足一个文件的data就丢失了 //问题在这里 和下面的写法有什么不同?
RandomAccessFile out;//像这样把变量的声明放在外面和放在循环里面有什么不同?
File outFile;
int j = 0;
// 根据要分割的数目输出文件
for (; j < value; j++) {
outFile = new File(inFile.getName() + j + ".txt");
out= new RandomAccessFile(outFile, "rw");
int temp = 0;
maxx += size; int i = 0;
while (i < maxx && (temp = inn.read()) != -1) {
if (temp > 127) { // 大于127表示读了半个汉字
count = ++count % 2; // 模2防止count溢出
}
out.write(temp);
i++;
}
if (count == 1 && (temp = inn.read()) != -1) { // count若为1表示最后读取了半个汉字
out.write(temp);
maxx++;
}
count = 0;
out.close();
} // 输出剩余的内容
outFile = new File(inFile.getName() + j + ".txt");
out = new RandomAccessFile(outFile, "rw");
int temp = 0;
while ((temp = inn.read()) != -1) {
out.write(temp);
}
out.close();
inn.close();
} public static void main(String[] args) throws Exception {
Fen cutt = new Fen("test.txt", 7);
cutt.cut();
}
}
写法2====================================================
import java.io.File;
import java.io.RandomAccessFile;public class Fen { String fileName;
int size; // size 分割成单独文件的大小
Fen(String fileName, int size) {
this.fileName = fileName;
this.size = size * 1024;
} public void cut() throws Exception { int maxx = 0;
File inFile = new File(fileName);
int count = 0; // 记录汉字数,一个汉字计数增加2
int fileLength = (int) inFile.length(); // 取得文件的大小
int value; // 取得要分割的个数 // 打开要分割的文件 //并将内容读入到 inn中
RandomAccessFile inn = new RandomAccessFile(inFile, "r"); value = fileLength / size;// 原本相除是小数的 但这个都是int型
// 所以如果只输出value个文件的话 后面的还剩的一些不足一个文件的data就丢失了
int j = 0;
// 根据要分割的数目输出文件
for (; j < value; j++) {
File outFile = new File(inFile.getName() + j + ".txt");
RandomAccessFile out= new RandomAccessFile(outFile, "rw");
int temp = 0;
maxx += size; int i = 0;
while (i < maxx && (temp = inn.read()) != -1) {
if (temp > 127) { // 大于127表示读了半个汉字
count = ++count % 2; // 模2防止count溢出
}
out.write(temp);
i++;
}
if (count == 1 && (temp = inn.read()) != -1) { // count若为1表示最后读取了半个汉字
out.write(temp);
maxx++;
}
count = 0;
out.close();
} // 输出剩余的内容
File outFile = new File(inFile.getName() + j + ".txt");
RandomAccessFile out2 = new RandomAccessFile(outFile, "rw");
int temp = 0;
while ((temp = inn.read()) != -1) {
out2.write(temp);
}
out2.close(); inn.close();
} public static void main(String[] args) throws Exception {
Fen cutt = new Fen("test.txt", 7);
cutt.cut();
}
}
在这里效果一样
RandomAccessFile out;只是做变量的声明,然后在循环中进行赋值。
RandomAccessFile out= new RandomAccessFile(outFile, "rw"); 在声明的同时做初始化。看个人的习惯。使用后者的话可以避免一些空指针异常的出现。
并不是说一定会出现空指针,只是说这样需要写代码的人注意一下,不要没赋值就使用了。P.S.
现在的IDE已经可以自动检测出这种情况并给出警告。所以一般也不会出现问题了。
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at Fen.cut(Fen.java:25)
at Fen.main(Fen.java:74)
啥意思呢?代码倒是没有错误
但每次都要重现分配空间,时间会有一点点损耗吧,不过我觉得这不算什么。RandomAccessFile out;只是做变量的声明,然后在循环中进行赋值。
RandomAccessFile out= new RandomAccessFile(outFile, "rw"); 在声明的同时做初始化。