import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 对一个大文件进行拆分为几个小文件
* @author jianhong
*
*/
public class SplitFile {
/**
* 对一个大文件进行拆分成多个小文件
* @param args 第一个参数设置:大文件的文件全路径 第二个参数设置:输出文件的路劲以及文件名(注意文件名称的后缀名在程序中进行设置,默认为: .txt文件后缀名,若要更改请在程序代码中进行更改)
*
第三个参数设置:拆分成小文件的总的个数 第四个参数设置 :每个小文件包含有多少条数据,最后一个小文件除外
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//先读大文件数据,即获取大文件的全路径
String path=args[0];
File file = new File(path);
//创建buffer字符输入流
BufferedReader br=null;
//创建文件输出流
FileWriter fw =null;
BufferedWriter bw =null;
if(() && ()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
br = new BufferedReader(isr);
String conditions = null;
String nums = args[2]; //输入拆分成小文件的总个数
int num = (nums);
int number =(args[3]); //设置每一个小文件应该包含多少条条数
String output=null;
int count=0;
//设置输出小文件的文件路径和小文件的文件名称
String out=args[1];
//创建小文件路径
for (int i = 0; i < num; i++) {
//output ="E:/工作表单/用户画像文件夹/output测试文件_"+i+".txt";
//拼接所有小文件的后缀名
output =out+"_000"+i+".txt";
//("输出第"+i+"文件:"+output);
File outfile=new File(output);
if (!()) {
boolean createFile = ();
if (createFile) {
("创建文件:"+output+"成功");
}else {
("文件创建失败!!!!!");
}
}
fw= new FileWriter(outfile, true);
bw= new BufferedWriter(fw);
//读大文件数据一行
while ((conditions = ()) != null) {
try {
(conditions);
("\r\n");
(conditions);
//判断是否达到小文件的条数
count++;
("这是第"+count+"条数据"); //count的值是1
} catch (IOException e) {
();
}
if (count == (i+1)*number ) {
("已完成第"+i+"文件");
break;
}
}
();
();
}
//判断这个拆分的小文件的个数是否合理
if (conditions !=null) {
count +=1;
if (count > num*number ) {
("警告:大文件的总条数大于拆分小文件的总条数,所读大文件的条数大于:"+(count-1)+"条;若数据非常重要请先删除小文件,重新设置小文件的个数,并重新进行拆分操作");
}
}else {
("当前已入条数为:"+count+"条");
}
}
();
}
}