10

我有将一些数据写入文件的方法。我用,PrintWriter如下图BufferedWriterFileWriter

public void writeToFile(String FileName){
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new BufferedWriter(new FileWriter(FileName)));
        for(Cars car : list){
            pw.println(car.getType());
            pw.println(car.getMaxSpeed());
            pw.println(car.getOwner());
            pw.println();
            pw.flush();
        }
        pw.close();
    }
    catch(IOException ex){
        System.err.println(ex);
    }
}

现在我怎样才能从文件中读取这些数据?我尝试使用InputStreamReader,BufferedReaderFileInputStream,但我的 NetBeans 显示一条错误消息

    public void readFromFile() throws IOException {
        InputStreamReader fr = null;
        try {
            fr = new InputStreamReader(new BufferedReader(new FileInputStream(new FileReader("c:\\cars.txt"))));
            System.out.println(fr.read());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            fr.close();
        }
    }  

这种方法有什么问题?

4

3 回答 3

15
BufferedReader in = new BufferedReader(new FileReader("file.in"));
BufferedWriter out = new BufferedWriter(new FileWriter("file.out"));

String line = in.readLine(); // <-- read whole line
StringTokenizer tk = new StringTokenizer(line);
int a = Integer.parseInt(tk.nextToken()); // <-- read single word on line and parse to int

out.write(""+a);
out.flush();
于 2013-04-21T20:41:12.490 回答
4

您的代码中有几个问题:

1) InputStreamReader 将 InputStream 作为参数而不是 Reader。请参阅http://docs.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html

2) FileInputStream 也不接受 Reader 作为参数(它接受 File、FileDescriptor 或 String)。请参阅:http ://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html

3) BufferedReader 正常逐行读取文件。read() 方法只读取一个字符。

一个可能的解决方案可能是:

fr = new BufferedReader(new InputStreamReader(new FileInputStream(new File("c:\\cars.txt"))));
String line = "";
while((line = fr.readLine()) != null) {
    System.out.println(line);
}

顺便说一句:如果您提供确切的错误消息甚至更好的 StackTrace,其他人会更容易帮助您。

于 2013-04-21T20:27:51.367 回答
1

简单错误:无法解析构造函数 'FileInputStream(java.io.FileReader)',API 中不存在所需的构造函数。

您的原始代码是:

new PrintWriter(new BufferedWriter(new FileWriter(FileName)));

所以为了阅读,你需要

new PrintReader(new BufferedReader(new FileReader(FileName)));

但不需要 PrintReader(不存在),所以您只需要:

new BufferedReader(new FileReader(FileName))

PrinterWriter 将对象的格式化表示打印到文本输出流,但在读取文本时始终是格式化的,因此 PrinterReader 不存在。

您正在逐行编写,因此也逐行阅读:)示例:

public void readFromFile() throws IOException {
    BufferedReader bufferedReader = null;
    try {
        String sCurrentLine;
        bufferedReader = new BufferedReader(new FileReader("c:\\cars.txt"));
        while ((sCurrentLine = bufferedReader.readLine()) != null) {
            System.out.println(sCurrentLine);
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    } finally {
        bufferedReader.close();
    }
}

或更好(JDK7)

void readFromFile() throws IOException {
    Path path = Paths.get("c:\\cars.txt");
    try (BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())){
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

    }
}
于 2013-04-21T20:32:54.047 回答