java文件读入流问题处理

java输出流写入

在使用mysql的select * from a into outfile 'E:\\a.csv' character set utf8,如果字段中有换行,会被转义成\\n,Tab转义为\\t,无法正常处理数据文件,使用java处理该文件,有一个现象,写入文件尾会出现’\u0000’乱码,乱码数量取决于定义的buff大小

try(
	BufferedReader br = 
			new BufferedReader(
				new InputStreamReader(
					new FileInputStream(filePath), "utf-8"),1024*1024*100);
	BufferedWriter bw = 
			new BufferedWriter (
				new OutputStreamReader(
					new FileOutputStream(tofilePath), "utf-8"),1024*1024*100);
){
	bw.write("");
	char[] buff = new char[10249*1024*10];
	//逐行读取会读取行错误
	while(br.read(buff)!=-1){
		String str = new String(buff);
		//正则替换所有\r
		str = str.replaceAll("\\r","");
		//正则替换\\n 
		str = str.replaceAll("\\\\\\n","");
		bw.append(str);
	}
}catch(IOException e){}
...

对代码进行修改,去掉文件尾乱码

try(
	BufferedReader br = 
			new BufferedReader(
				new InputStreamReader(
					new FileInputStream(filePath), "utf-8"),1024*1024*100);
	BufferedWriter bw = 
			new BufferedWriter (
				new OutputStreamReader(
					new FileOutputStream(tofilePath), "utf-8"),1024*1024*100);
){
	bw.write("");
	int N = 10249*1024*10;
	char[] buff = new char[N];
	int len = 0;
	//逐行读取会读取行错误
	while((len=br.read(buff))!=-1){
		String str = new String(buff,0,len);
		//正则替换所有\r
		str = str.replaceAll("\\r","");
		//正则替换\\n 
		str = str.replaceAll("\\\\\\n","");
		bw.append(str);
		len = 0;
	}
}catch(IOException e){}
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值