tsvico的博客

好的代码像粥一样,都是用时间熬出来的

java文件读入流问题处理

java输出流写入

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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){}
...

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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){}
...
坚持原创技术分享,您的支持将鼓励我继续创作!