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

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

问题描述

很抱歉,如果这是一个显而易见的问题,但我似乎无法理解.我正在为数据结构"课程做作业.它涉及从简单的.dat文件中提取数据.我们以前从未使用过Java中的任何文件访问选项,因此教授只是为我们提供了该文件的工作代码.

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.

一个名为FileReadExample的类创建一个新的BufferedReader对象,打开一个文件,然后将其踢出一堆有关该文件的数据. 但是我根本无法访问任何数据.

A class called FileReadExample creates a new BufferedReader 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.

在一个单独的testMain文件中,我创建了一个名为fr的新FileReadExample对象,然后尝试从该位置打印出fr.readLine()之类的东西,但它告诉我没有这种方法.

In a separate testMain file, I created a new FileReadExample object named fr and then attempted 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!");
        }
    }
}

谢谢.

推荐答案

尝试使用此方法读取文件:

Try this to read a file:

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中使用BufferedReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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