java按行分割文件名_JAVA-分割大文件(按行分割)

packagecom.testSplitByNumber;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.nio.charset.Charset;importjava.util.HashMap;importjava.util.Map;importcom.csvreader.CsvReader;public classSplitFile {public static voidmain(String args[]){

String path= "F:/zh.csv";

String folder= path.substring(0, path.lastIndexOf("."));

File file= newFile(path);

File file2= newFile(folder);

Map map = new HashMap();if(!file.exists()){

System.out.println("文件不存在!");

}else{if(! file2.exists() || !file2.isDirectory()){

file2.mkdir();

}long start =System.currentTimeMillis();

CsvReader reader= null;

BufferedWriter bw= null;try{

reader= new CsvReader(path, ',', Charset.forName("utf-8"));

reader.readHeaders();int count = 0;while(reader.readRecord()){

count++;

String strs[]=reader.getValues();

String line= strs[strs.length - 1];

String fileName= folder + File.separator + file.getName().subSequence(0, file.getName().lastIndexOf("."))+ line.charAt(line.length() - 1)+".csv";

bw=map.get(fileName);if(bw == null){

bw= new BufferedWriter(new FileWriter(newFile(fileName)));

}

map.put(fileName, bw);

bw.write(writeLine(strs)+"\r\n");if(count % 100000 == 0){

bw.flush();

System.out.println("已经处理:" + count + "条");

}

}long end =System.currentTimeMillis();

System.out.println("共用时:" + (end - start)/1000 + "s");

}catch(IOException e) {

e.printStackTrace();

}finally{if(reader != null){

reader.close();

reader= null;

}for(BufferedWriter bww : map.values()){try{

bww.flush();

bww.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

}public staticString writeLine(String strs[]){

StringBuffer sb= new StringBuffer("");for(int i = 0;i < strs.length;i++){

strs[i]= strs[i].replaceAll(",", ",");

strs[i]= strs[i].replaceAll("\\s*", "");

sb.append(strs[i]+ ",");

}return sb.substring(0, sb.length() - 1).toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值