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){}
...