// oriFileName 原文件名称 controlNum 每多少条生成一个文件 返回新生成的文件名字符串数组
public String[] splitFile(String oriFileName, int controlNum) {
String[] res = new String[100];
try {
FileReader read = new FileReader(oriFileName + ".dat");
BufferedReader br = new BufferedReader(read);
String row;
int curRow = 0;
int fileNo = 1;
FileWriter fw = new FileWriter(oriFileName + fileNo + ".dat");
res[fileNo - 1] = oriFileName + (fileNo) + ".dat";
while ((row = br.readLine()) != null) {
curRow++;
fw.append(row + "\r\n");
// 判断是否生成一个新的文件
if ((curRow / controlNum) > (fileNo - 1)) {
fw.close();
fileNo++;
fw = new FileWriter(oriFileName + fileNo + ".dat");
res[fileNo - 1] = oriFileName + fileNo + ".dat";
}
}
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
java将大文件转成小文件并以数组返回
最新推荐文章于 2023-06-29 18:51:55 发布