使用TextIO类从文本文件中读取?

时间:2017-02-08 11:41:29

标签: java filereader readfile

赋值是读取字符串行和文本文件中的三个int行,这些文本文件看起来像这样(行之间没有空格):

Alex Miller

10

8

6

为此,我必须使用TextIO类来读取文件,该类有一堆静态方法(书中的子例程指的是它们)用于输入/输出。以下是我TextIO方法的readLine()类的代码段,我尝试使用该方法,但未成功。

     /**
    * Opens a file with a specified name for input.  If the file name is null, this has
    * the same effect as calling readStandardInput(); that is, input will be read from standard
    * input.  If an
    * error occurs while trying to open the file, an exception of type IllegalArgumentException
    * is thrown, and the input source is not changed.  If the file is opened 
    * successfully, then after this method is called, all of the input routines will read 
    * from the file, instead of from standard input.
    */
   public static void readFile(String fileName) {
      if (fileName == null) // Go back to reading standard input
         readStandardInput();
      else {
         BufferedReader newin;
         try {
            newin = new BufferedReader( new FileReader(fileName) );
         }
         catch (Exception e) {
            throw new IllegalArgumentException("Can't open file \"" + fileName + "\" for input.\n"
                           + "(Error :" + e + ")");
         }
         if (! readingStandardInput) { // close current input stream
            try {
               in.close();
            }
            catch (Exception e) {
            }
         }
         emptyBuffer();  // Added November 2007
         in = newin;
         readingStandardInput = false;
         inputErrorCount = 0;
         inputFileName = fileName;
      }
   }

这就是我想要做的事情:

public class ReadingFromFile {

    public static void main(String[] args) {
        String name;     // The student's name, from the first line of the file.
           int exam1, exam2, exam3;     // The student's grades on the three exams.
           double average;  // The average of the three exam grades.

           TextIO.readFile("testdata.txt");  // Read from the file.

           name = TextIO.getln();  // Reads the entire first line of the file.
           exam1 = TextIO.getlnInt();
           exam2 = TextIO.getlnInt();
           exam3 = TextIO.getlnInt();

           average = ( exam1 + exam2 + exam3 ) / 3.0;

           System.out.printf("The average grade for %s was %1.1f", name, average);
           System.out.println();
    }
}

但是我收到了这条消息:

线程中的异常" main" java.lang.IllegalArgumentException:无法打开文件" testdata.txt"输入。 (错误:java.io.FileNotFoundException:testdata.txt(系统找不到指定的路径))     在TextIO.readFile(TextIO.java:133)     在ReadingFromFile.main(ReadingFromFile.java:9)

这个TextIO.java:133实际上是

 throw new IllegalArgumentException("Can't open file \"" + fileName + "\" for input.\n"
                       + "(Error :" + e + ")");

我还尝试输入datatest.text的路径文件夹名称,如下所示:

 TextIO.readFile("src/default package/testdata.txt");

但我又得到了同样的例外。

如何使用此类正确读取文件?

1 个答案:

答案 0 :(得分:0)

所以我测试了你的代码片段:

public static void main(String[] args) {
    String name;     // The student's name, from the first line of the file.
    int exam1, exam2, exam3;     // The student's grades on the three exams.
    double average;  // The average of the three exam grades.

    TextIO.readFile("C:/Users/username/Desktop/test.txt");  // Read from the file.

    name = TextIO.getln();  // Reads the entire first line of the file.
    exam1 = TextIO.getlnInt();
    exam2 = TextIO.getlnInt();
    exam3 = TextIO.getlnInt();

    average = ( exam1 + exam2 + exam3 ) / 3.0;

    System.out.printf("The average grade for %s was %1.1f", name, average);
    System.out.println();
}

文本文件看起来像这样

asd
2
3
4

输出为:The average grade for asd was 3.0

你复制了while TextIO类吗?

相关问题