文件读取:获取部分输出

发布于 2025-01-04 20:23:07 字数 769 浏览 1 评论 0原文

我有以下代码来从我的文件中检索数据。当我执行代码时,我发现它只给出了总行数的 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();
        }
    }

i have the following code to retrive the data from my file. When i execute the code i come to know it's giving only 50% of the lines from the total lines. Why it's happening ?

    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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2025-01-11 20:23:07

你读了两行:

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

}

但你只数了一次。因此,您实际上正在读取整个文件,但只计算了一半的行。

将其更改为:

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

看看会发生什么。

You're reading the lines twice:

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

}

but you only count them once. So you're actually reading the whole file, but counting only half of the lines.

Change it to:

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

and see what happens.

李不 2025-01-11 20:23:07

因为

while(bufRead.readLine() != null)

丢弃了刚刚读取的行。

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

Because

while(bufRead.readLine() != null)

discards the line it just read.

String myLine = null;
while ((myLine = bufRead.readLine()) != null) {
    System.out.println("Count "+count+" : " + myLine);
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文