/**
* 把文件按照设置的大小分割成多个小的文件,解析后的多个文件,是在原文件后加上序号
*
* @param fileName 原始的文件
* @param size 分割后每个文件的大小,有可能大于这个值,
* 因为在解析的时候,是读取原始文件的一行进行解析的
* @throws IOException
*/
public static void splitFile(String fileName, int size) throws IOException
{
File file = new File(fileName);
if (!file.exists() || !file.canRead())
{
System.out.println("file not exist");
return;
}
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
String path = fileName.substring(0, fileName.lastIndexOf("/") + 1);
String name = fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.lastIndexOf("."));
int count = 1;
File fileW = new File(path + name + count + ".txt");
if (!fileW.exists())
{
fileW.createNewFile();
}
FileWriter fw = new FileWriter(fileW);
BufferedWriter bw = new BufferedWriter(fw);
int fileSize = 0;
// System.out.println("name : " + name + " path : " + path) ;
while (null != str)
{
if (fileSize >= size)
{
// 重新生成小的文件
bw.close();
fw.close();
count += 1;
fileW = new File(path + name + count + ".txt");
if (!fileW.exists())
{
fileW.createNewFile();
}
fw = new FileWriter(fileW);
bw = new BufferedWriter(fw);
fileSize = 0;
}
fileSize += str.getBytes().length;
bw.write(str + "\n");
str = br.readLine();
}
bw.close();
fw.close();
}