如何在Java中使用Buffered Reader [英] How to use Buffered Reader in Java

查看:115
本文介绍了如何在Java中使用Buffered Reader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个显而易见的问题,但我似乎无法得到它。我正在为数据结构课程做作业。它涉及从简单的.dat文件中提取数据。我们之前从未使用过Java中的任何文件访问选项,所以教授只是给了我们这篇文章的工作代码。因此,一个名为'FileReadExample'的类创建一个新的缓冲读取器对象,打开一个文件,然后应该发出一堆关于该文件的数据。但我根本无法访问任何数据。在一个单独的testMain文件中,我创建了一个名为fr的新FileReadExample对象,然后尝试从那里打印出像fr.readLine()这样的东西,但它告诉我没有这样的方法。我确定我错过了一些令人难以置信的事情。

Sorry if this is an obvious question, but I can't seem to get it. I'm working on an assignment for a Data Structures course. It involves pulling data from a simple .dat file. We had never used any of the file-accessing options in Java before, so the professor just gave us the working code for that piece. So a class called 'FileReadExample' creates a new buffered reader object, opens a file, and then is supposed to kick out a bunch of data about that file. But I cannot access any of the data at all. In a separate testMain file, I create a new FileReadExample object named fr and then attempt to print out things like fr.readLine() from there, but it tells me there is no such method. I'm sure I'm missing something staggeringly easy.

编辑 - 教授代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReadExample
{
    public static void main(String[] args)
    {
        System.out.println("got here");
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(new File("sample-file.dat")));
            System.out.println("File open successful!");

            int line = 0;
            for (String x = in.readLine(); x != null; x = in.readLine())
            {
                line++;
                System.out.println(x);
                if (line <= 3)
                {
                    String[] tokens = x.split(" ");
                    System.out.println("Number of tokens in line " + line + ": " + tokens.length);
                    System.out.println("The tokens are:");
                    for (String token : tokens)
                    {
                        System.out.println(token);
                    }
                }
                else
                {
                    String[] tokens = x.split("\\|");
                    System.out.println("Number of tokens in line " + line + ": " + tokens.length);
                    System.out.println("The tokens are:");
                    for (String token : tokens)
                    {
                        System.out.println(token);
                    }
                    Integer[] values = new Integer[tokens.length];
                    Integer sum = 0;
                    for (int i = 0; i < tokens.length; i++)
                    {
                        sum += Integer.parseInt(tokens[i]);
                    }
                    System.out.println("Sum: " + sum);
                }
            }
        } catch (IOException e)
        {
            System.out.println("File I/O error!");
        }
    }
}

我可以发布代码教授给了,但我无法弄清楚如何在这些帖子中获得格式。我是否正确阅读,我必须在每行前面放置四个空格,以便在此处正确格式化代码?有没有办法一次阻止一大堆代码?再一次,我觉得我错过了一些非常容易的东西。

I can post the code the professor gave, but I can't figure out how to get the formatting right in these posts. Am I reading right that I have to put four spaces in front of every line to get the code formatting properly here? There is no way to just block a whole bunch of code at once? Again, I feel like I am missing something extremely easy there.

谢谢。

推荐答案

试试这个来读取文件:

BufferedReader reader = null;

try {
    File file = new File("sample-file.dat");
    reader = new BufferedReader(new FileReader(file));

    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这篇关于如何在Java中使用Buffered Reader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆