如何在 Java 中使用 BufferedReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16265693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use BufferedReader in Java
提问by Christopher Robinson
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.
对不起,如果这是一个明显的问题,但我似乎无法理解。我正在处理数据结构课程的作业。它涉及从一个简单的 .dat 文件中提取数据。我们以前从未使用过 Java 中的任何文件访问选项,因此教授只给了我们该部分的工作代码。
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.
一个名为的类FileReadExample
创建一个新BufferedReader
对象,打开一个文件,然后应该踢出一堆关于该文件的数据。但我根本无法访问任何数据。
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.
在一个单独的testMain
文件中,我创建了一个FileReadExample
名为的新对象fr
,然后尝试fr.readLine()
从那里打印出类似的东西,但它告诉我没有这样的方法。
I'm sure I'm missing something staggeringly easy.
我确定我错过了一些非常简单的东西。
The professor's code:
教授代码:
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!");
}
}
}
Thanks.
谢谢。
回答by Matthias Herlitzius
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();
}
}
回答by Onkar Borgaonkar
As far as i understand fr is the object of your FileReadExample class. So it is obvious it will not have any method like fr.readLine() if you dont create one yourself.
据我了解 fr 是您的 FileReadExample 类的对象。所以很明显,如果你不自己创建,它不会有任何像 fr.readLine() 这样的方法。
secondly, i think a correct constructor of the BufferedReader class will help you do your task.
其次,我认为 BufferedReader 类的正确构造函数将帮助您完成任务。
String str;
BufferedReader buffread = new BufferedReader(new FileReader(new File("file.dat")));
str = buffread.readLine();
.
.
buffread.close();
this should help you.
这应该可以帮助你。