简体   繁体   English

如何使用BufferReader和PrintWriter组合来输入文本

[英]How to use a BufferReader and PrintWriter combo to input text

I am going to participate in USACO later this year, and I am most likely going to use Java. 我将在今年晚些时候参加USACO,我很可能会使用Java。 However, I have not covered File I/O thoroughly. 但是,我没有彻底涵盖文件I / O. USACO insists that we use this BufferReader, PrintWriter, and StringTokenizer combination in order to parse inputted text. USACO坚持认为我们使用这个BufferReader,PrintWriter和StringTokenizer组合来解析输入的文本。 Here is the code that they showed: 这是他们展示的代码:

import java.io.*;
import java.util.*;

class test {
  public static void main (String [] args) throws IOException {
    // Use BufferedReader rather than RandomAccessFile; it's much faster
    BufferedReader f = new BufferedReader(new FileReader("test.in"));
                                              // input file name goes above
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));
    // Use StringTokenizer vs. readLine/split -- lots faster
    StringTokenizer st = new StringTokenizer(f.readLine());
                      // Get line, break into tokens
    int i1 = Integer.parseInt(st.nextToken());    // first integer
    int i2 = Integer.parseInt(st.nextToken());    // second integer
    out.println(i1+i2);                           // output result
    out.close();                                  // close the output file
    System.exit(0);                               // don't omit this!
  }
}

However, when I literally copy/pasted the code into Netbeans, it didn't run. 但是,当我将代码复制/粘贴到Netbeans中时,它没有运行。 It gave a FileNotFoundException at the line "BufferedReader f = new BufferedReader..." I suppose it's because of the "test.out", but I am not sure what to put in it. 它在“BufferedReader f = new BufferedReader ...”行给出了一个FileNotFoundException,我想这是因为“test.out”,但我不知道该放入什么。 How do I fix this? 我该如何解决?

Additionally, USACO said that this was the most efficient way to parse inputted text. 此外,USACO表示这是解析输入文本的最有效方式。 Is this true? 这是真的? As in, is this really the most efficient way to retrieve inputted text. 如此,这是检索输入文本的最有效方法。 I am familiar with other ways like with the Scanner class, but USACO insists that using BufferedReader, PrintWriter, etc. is the best way to do it. 我熟悉Scanner类等其他方法,但USACO坚持使用BufferedReader,PrintWriter等是最好的方法。

StringTokenizer is more efficient than Scanner and split . StringTokenizerScannersplit更有效。 Both Scanner and split use regex to tokenize their input. Scannersplit使用正则表达式来标记其输入。 StringTokenizer does not use regex, and therefore doesn't run into the overhead of using it. StringTokenizer不使用正则表达式,因此不会遇到使用它的开销。

test.in is a file in the working directory of your project: test.in是项目工作目录中的文件:

YourProject
    src
        test.java
    bin 
        test.class
    test.in

I did USACO back in the day with pure Java. 我用纯Java做了USACO。 (I did really well.) (我做得很好。)

You almost always want to use Scanner -- it's pretty much perfect for programming competitions, and I/O usually isn't going to be your bottleneck. 你几乎总是想使用Scanner - 它非常适合编程比赛,而I / O通常不会成为你的瓶颈。 StringTokenizer is a bit more efficient, but you should only really worry about that when your program is going to be linear-time anyway. StringTokenizer效率更高,但是当你的程序无论如何都是线性时,你应该真正担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM