最近写了一个读取文本文件的java程序,如下
import java.io.*;
import java.lang.*;
public class Asm{
public static void main(String args[]) throws IOException{
FileInputStream fis=new FileInputStream("E:\\"+args[0]+".txt");
BufferedReader buff = new BufferedReader(new InputStreamReader(fis)); 
int count=0;//统计输入行数
do{
String strRead=buff.readLine();
if(strRead==null){
if (count==0){
System.out.println("打开文件为空");
break;
}
else {
System.out.println("读取文件结束");
break;
}
}
System.out.println(strRead);
count++;
}while(buff.readLine()!=null);
System.out.println("count="+count);
fis.close();
buff.close();
}
}
=============================
可是读取文本文件的时候,却是跳行读取的?!
请教问题出在什么地方?谢谢
/*示例文本如下*/
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*显示的计数值只有50?!*/

解决方案 »

  1.   

    因为每次循环都调用readLine()两次,,所以看上去就是隔行了。改成这样:
    int count=0;//统计输入行数
    String strRead=buff.readLine();
    do{

    if(strRead==null){
    if (count==0){
    System.out.println("打开文件为空");
    break;
    }
    else {
    System.out.println("读取文件结束");
    break;
    }
    }
    System.out.println(strRead);
    count++;
    }while(buff.readLine()!=null);
      

  2.   

    你一次循环用了两个 buff.readLine() ,有一个没输出呢。BufferedReader buff = new BufferedReader(new InputStreamReader(fis)); 
    int count=0;//统计输入行数
    String str = "";
    while((str = buff.readLine())!=null){
       if (count==0){
          System.out.println("打开文件为空");
          break;
       }
       else {
          System.out.println("读取文件结束");
          break;
       }
    }
    System.out.println(str);
    count++;
    }