java按行分割大文件

/**
	 * 按行分割文件
	 * @param rows 为多少行一个文件 
	 * @param sourceFilePath 为源文件路径 
	 * @param targetDirectoryPath 文件分割后存放的目标目录
	 */
	public void splitDataToSaveFile(int rows, String sourceFilePath,
			String targetDirectoryPath) {
		long start1 = System.currentTimeMillis();
		
		File sourceFile = new File(sourceFilePath);
		File targetFile = new File(targetDirectoryPath);
		if (!sourceFile.exists() || rows <= 0 || sourceFile.isDirectory()) {
			return;
		}
		if (targetFile.exists()) {
			if (!targetFile.isDirectory()) {
				return;
			}
		} else {
			targetFile.mkdirs();
		}
		try {

			InputStreamReader in = new InputStreamReader(new FileInputStream(sourceFilePath),"GBK");
			BufferedReader br=new BufferedReader(in);
			
			BufferedWriter bw = null;
			String str = "";
			String tempData = br.readLine();
			int i = 1, s = 0;
			long start2 = System.currentTimeMillis();
			while (tempData != null) {
				str += tempData + "\r\n";
				if (i % rows == 0) {
					bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( 
							targetFile.getAbsolutePath() + "/" +  sourceFile.getName() +"_" + (s+1) +".csv"), "GBK"),1024); 
					
					bw.write(str);
					bw.close();
					
					str = "";
					start2 = System.currentTimeMillis();
					s += 1;
				}
				i++;
				tempData = br.readLine();
			}
			if ((i - 1) % rows != 0) {
				
				bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( 
						targetFile.getAbsolutePath() + "/" +  sourceFile.getName() +"_" + (s+1) +".csv"), "GBK"),1024); 
				bw.write(str);
				bw.close();
				br.close();
				
				s += 1;
			}
			in.close();
			
		} catch (Exception e) {
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值