这是我用来读一段数据的代码,并且把每列用"\t"分隔。
public class ParseFileTxt { FileInputStream fi = null;
BufferedReader br = null;
String newFilePath = "";
File newFile = null;
FileWriter fw = null;
BufferedWriter bw = null;
public ParseFileTxt() throws Exception {
fi = new FileInputStream("D:\\file\\wttick.txt");
br = new BufferedReader(new InputStreamReader(fi,"GB2312"));
newFilePath = "D:\\file\\NewFile\\newWttick.txt";
newFile = new File(newFilePath);
fw = new FileWriter(newFile, true);
bw = new BufferedWriter(fw);
}
public static void main(String[] args) throws Exception{
ParseFileTxt parseFile = new ParseFileTxt();
parseFile.parseFile();
}
private void parseFile()throws Exception {
String temp = "";
while(true){
String line ="";
byte[] cbuf = new byte[1024 * 256];
//首先读取数据241个字节
int num = fi.read(cbuf, 0, 272);
if(num<0){
break;
}
//首先读取数据241个字节
String str1 = new String(cbuf,0,15 , "GB2312").trim();
String str2 = new String(cbuf,17,20, "GB2312").trim();
String str3 = new String(cbuf,38,10 , "GB2312").trim();
String str4 = new String(cbuf,49,10 , "GB2312").trim();
String str5 = new String(cbuf,60,12 , "GB2312").trim();
String str6 = new String(cbuf,80,17 , "GB2312").trim();
String str7 = new String(cbuf,101,11 , "GB2312").trim();
String str8 = new String(cbuf,113,9 , "GB2312").trim();
String str9 = new String(cbuf,124,13 , "GB2312").trim();
String str10 = new String(cbuf,143,9 , "GB2312").trim();
String str11 = new String(cbuf,155,7, "GB2312").trim();
String str12 = new String(cbuf,164,8 , "GB2312").trim();
String str13 = new String(cbuf,174,16 , "GB2312").trim();
String str14 = new String(cbuf,195,15 , "GB2312").trim();
String str15 = new String(cbuf,215,27 , "GB2312").trim();
line = str1 + "\t" + str2 + "\t" +str3 + "\t" +str4 + "\t" +str5 + "\t" +str6
+ "\t" +str7 + "\t" +str8 + "\t" +str9 + "\t" +str10 + "\t" +str11 + "\t" +str12
+ "\t" +str13 + "\t" +str14 + "\t" + str15 ;
bw.write(line);
bw.newLine();
System.out.println(line);
bw.flush();
}
}
这里是数据
112.5.235.135 其他客户端 20130513 00:03:14 13075997362 取得预设帐号 00:03:14 处理成功
112.5.235.135 Gphone手机 20130513 00:03:22 6097976 客户校验 00:03:22 处理成功 13075997362
117.136.17.56 其他客户端 20130513 00:03:29 13908517304 取得预设帐号 00:03:29 处理成功
117.136.17.56 Gphone手机 20130513 00:03:37 66155 客户校验 00:03:37 处理成功 13908517304
211.139.92.12 其他客户端 20130513 00:06:03 18293613691 取得预设帐号 00:06:03 处理成功
211.139.92.12 Gphone手机 20130513 00:06:11 10071877 客户校验 00:06:11 处理成功 18293613691
117.136.39.65 其他客户端 20130513 00:06:49 15012745498 取得预设帐号 00:06:49 处理成功
117.136.39.65 Gphone手机 20130513 00:07:19 80040879 客户校验 00:07:19 处理成功 15012745498
202.100.77.210 Gphone手机 20130513 00:08:12 10037730 委托买入 00:08:12 处理成功 15309475358 600609 3.84 2000 A553975592 1746 1746
117.136.9.60 mobile手机 20130513 00:09:45 15910136780 取得预设帐号 00:09:45 处理成功 15910136780
117.136.9.60 mobile手机 20130513 00:09:46 15910136780 取得预设帐号 00:09:46 处理成功 15910136780 00:12:45 处理成功 这些数据不是以tab分隔的,每一行的bytes都是272个。就是这里的num 肯定是272个,这个没有疑问。
并且我把当中的核心代码抽出来,一行一行的读,都能得到我想要的效果,如果一起读,从第二行开始就出现错误了,我不知道哪里错了 ,暂时还没找出原因来!!!
错误是这样的:
112.5.235.135 其他客户端 20130513 00:03:14 13075997362 取得预设帐号 00:03:14 处理成功
112.5.235.135 Gphone手机 2013051 00:03:2 6097976 客户校验 00:03:2 处理成 1307599736
117.136.17. 其他客户端 20130 13 00:03 29 1390851 取得预设帐? 00:03 29 处理 功
117.136.1 56 Gphone手机 201 0513 00: 3:37 66155 客户校验 00: 3:37 处 成功 139085
211.139 2.12 其他客户端 2 130513 0 :06:03 182 取得预? ? 0 :06:03 理成功
211.1 .92.12 Gphone手? 20130513 00:06:11 1 客户? 00:06:11 处理成功 18 691
117 36.39.65 其他客? 端 20130513 00:06:49 5498 取? 枵屎? 00:06:4 处理成功
1 .136.39.65 Gphon 手机 201305 3 00:07:19 879 ? Q? 00:07 9 处理成功 2745498 从第二行开始 很多地方都已经不对了,不知道什么情况,找了半天没找出来。。IO流JavaStringj2se
public class ParseFileTxt { FileInputStream fi = null;
BufferedReader br = null;
String newFilePath = "";
File newFile = null;
FileWriter fw = null;
BufferedWriter bw = null;
public ParseFileTxt() throws Exception {
fi = new FileInputStream("D:\\file\\wttick.txt");
br = new BufferedReader(new InputStreamReader(fi,"GB2312"));
newFilePath = "D:\\file\\NewFile\\newWttick.txt";
newFile = new File(newFilePath);
fw = new FileWriter(newFile, true);
bw = new BufferedWriter(fw);
}
public static void main(String[] args) throws Exception{
ParseFileTxt parseFile = new ParseFileTxt();
parseFile.parseFile();
}
private void parseFile()throws Exception {
String temp = "";
while(true){
String line ="";
byte[] cbuf = new byte[1024 * 256];
//首先读取数据241个字节
int num = fi.read(cbuf, 0, 272);
if(num<0){
break;
}
//首先读取数据241个字节
String str1 = new String(cbuf,0,15 , "GB2312").trim();
String str2 = new String(cbuf,17,20, "GB2312").trim();
String str3 = new String(cbuf,38,10 , "GB2312").trim();
String str4 = new String(cbuf,49,10 , "GB2312").trim();
String str5 = new String(cbuf,60,12 , "GB2312").trim();
String str6 = new String(cbuf,80,17 , "GB2312").trim();
String str7 = new String(cbuf,101,11 , "GB2312").trim();
String str8 = new String(cbuf,113,9 , "GB2312").trim();
String str9 = new String(cbuf,124,13 , "GB2312").trim();
String str10 = new String(cbuf,143,9 , "GB2312").trim();
String str11 = new String(cbuf,155,7, "GB2312").trim();
String str12 = new String(cbuf,164,8 , "GB2312").trim();
String str13 = new String(cbuf,174,16 , "GB2312").trim();
String str14 = new String(cbuf,195,15 , "GB2312").trim();
String str15 = new String(cbuf,215,27 , "GB2312").trim();
line = str1 + "\t" + str2 + "\t" +str3 + "\t" +str4 + "\t" +str5 + "\t" +str6
+ "\t" +str7 + "\t" +str8 + "\t" +str9 + "\t" +str10 + "\t" +str11 + "\t" +str12
+ "\t" +str13 + "\t" +str14 + "\t" + str15 ;
bw.write(line);
bw.newLine();
System.out.println(line);
bw.flush();
}
}
这里是数据
112.5.235.135 其他客户端 20130513 00:03:14 13075997362 取得预设帐号 00:03:14 处理成功
112.5.235.135 Gphone手机 20130513 00:03:22 6097976 客户校验 00:03:22 处理成功 13075997362
117.136.17.56 其他客户端 20130513 00:03:29 13908517304 取得预设帐号 00:03:29 处理成功
117.136.17.56 Gphone手机 20130513 00:03:37 66155 客户校验 00:03:37 处理成功 13908517304
211.139.92.12 其他客户端 20130513 00:06:03 18293613691 取得预设帐号 00:06:03 处理成功
211.139.92.12 Gphone手机 20130513 00:06:11 10071877 客户校验 00:06:11 处理成功 18293613691
117.136.39.65 其他客户端 20130513 00:06:49 15012745498 取得预设帐号 00:06:49 处理成功
117.136.39.65 Gphone手机 20130513 00:07:19 80040879 客户校验 00:07:19 处理成功 15012745498
202.100.77.210 Gphone手机 20130513 00:08:12 10037730 委托买入 00:08:12 处理成功 15309475358 600609 3.84 2000 A553975592 1746 1746
117.136.9.60 mobile手机 20130513 00:09:45 15910136780 取得预设帐号 00:09:45 处理成功 15910136780
117.136.9.60 mobile手机 20130513 00:09:46 15910136780 取得预设帐号 00:09:46 处理成功 15910136780 00:12:45 处理成功 这些数据不是以tab分隔的,每一行的bytes都是272个。就是这里的num 肯定是272个,这个没有疑问。
并且我把当中的核心代码抽出来,一行一行的读,都能得到我想要的效果,如果一起读,从第二行开始就出现错误了,我不知道哪里错了 ,暂时还没找出原因来!!!
错误是这样的:
112.5.235.135 其他客户端 20130513 00:03:14 13075997362 取得预设帐号 00:03:14 处理成功
112.5.235.135 Gphone手机 2013051 00:03:2 6097976 客户校验 00:03:2 处理成 1307599736
117.136.17. 其他客户端 20130 13 00:03 29 1390851 取得预设帐? 00:03 29 处理 功
117.136.1 56 Gphone手机 201 0513 00: 3:37 66155 客户校验 00: 3:37 处 成功 139085
211.139 2.12 其他客户端 2 130513 0 :06:03 182 取得预? ? 0 :06:03 理成功
211.1 .92.12 Gphone手? 20130513 00:06:11 1 客户? 00:06:11 处理成功 18 691
117 36.39.65 其他客? 端 20130513 00:06:49 5498 取? 枵屎? 00:06:4 处理成功
1 .136.39.65 Gphon 手机 201305 3 00:07:19 879 ? Q? 00:07 9 处理成功 2745498 从第二行开始 很多地方都已经不对了,不知道什么情况,找了半天没找出来。。IO流JavaStringj2se
这段代码中:
byte[] cbuf = new byte[1024 * 256];
//首先读取数据241个字节
int num = fi.read(cbuf, 0, 272);fi.read(cbuf, 0, 272)读本地文件有可能是272个字节,但如果读的是网络流他就不一定是272个字节,num才是真实读多少个字节的数量。
每行结束有行结束符,你没计算在内。
所以每次 read,都算到下一个buf里面,造成定位不准确。
一个换行符,占2个byereadLine,会忽略换行符,而采用null结尾。所以看到长度是272
int num = fi.read(cbuf, 0, 274);
我知道是这个问题 查了一下"\n"是占一个字符,所以我改成了273 ,发现还是有问题,不知道为什么是274。 然道"\n"占了2个字符?嗯,\n 是一个 char,占2个字节。注意是byte! 你是按byte来读的不是按char来读的。