文件读取:获取部分输出

Aru*_*ung 1 java file filestream

我有以下代码来检索我的文件中的数据.当我执行代码时,我知道它只给出了总线数的50%.为什么会这样?

    public static void main(String args[]) throws IOException
    {
        int count = 1;
    try {
            FileInputStream fileInput = new FileInputStream("C:/FaceProv.log");
            DataInputStream dataInput = new DataInputStream(fileInput);
            InputStreamReader inputStr = new InputStreamReader(dataInput);
            BufferedReader bufRead = new BufferedReader(inputStr);

                while(bufRead.readLine() != null)
                {
                    System.out.println("Count "+count+" : "+bufRead.readLine());
                    count++;

                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

sou*_*eck 6

你正在阅读这两行:

while(bufRead.readLine() != null) /// HERE
{
     System.out.println("Count "+count+" : "+bufRead.readLine()); // AND HERE
     count++;

}
Run Code Online (Sandbox Code Playgroud)

但你只计算一次.所以你实际上是在阅读整个文件,但只计算一半的行数.

将其更改为:

String line;
while((line = bufRead.readLine()) != null) {
     System.out.println("Count "+count+" : " + line);
     count++;
}
Run Code Online (Sandbox Code Playgroud)

看看会发生什么.