1

我正在为我的编程期末考试而学习。我必须编写一个程序来打开一个存储在字符串 fileName 中的文件并在文件中查找名为 personName 的字符串,这应该打印 personName 之后的第一个字符串,然后程序应该在打印后终止,如果参数 personName 是不在文件中,则应打印“此名称不存在”,然后如果发生 IOException,则应打印“存在 IO 错误”,并且程序应使用 system.exit(0) 退出

程序应该使用文件 info.txt 并且每行应该包含两个字符串第一个字符串名称和第二个年龄。

一切都必须在一种方法中

data.txt 包含

最大 60.0

乔 19.0

阿里20.0

到目前为止,我的代码是:

public class Files{

    public void InfoReader(String fileName, String personName)
    {

      try{
         try{
                   // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("C://rest//data.txt");
                  // Get the object of DataInputStream

                  DataInputStream in = new DataInputStream(fstream);
                  BufferedReader br = new BufferedReader(new InputStreamReader(in));

                  //Read File Line By Line
                  while ((fileName = br.readLine()) != null) {
                      // Print the content on the console
                      (new Files()).infoReader("info.txt","Joe"); //this prints the age
                  }
                  //Close the input stream
                  in.close();
              }

              catch (IOException e)
              {//Catch exception if any
                    System.out.println(" there is an IO Error");
                    System.exit(0);
              }
     }
    catch (Exception e)
              {//Catch exception if any
                    System.out.println("that name doesn't exists");

              }
    }
}

infoReader(info.txt,Joe);应该打印19.0
但我得到一个java.lang.StackOverflowError

任何帮助将非常感激!!

提前致谢!

4

3 回答 3

1

这就是我认为你正在尝试做的事情。如果没有,至少可以作为一个例子。正如阿米特所提到的,您当前的错误是由于递归调用,我认为这不是必需的。

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Files {

    public void InfoReader(String fileName, String personName) {
        try {
            // Open the file that is the first command line parameter
            FileInputStream fstream = new FileInputStream(fileName);

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String line = null;

            //Loop until there are no more lines in the file
            while((line = br.readLine()) != null) {
                //Split the line to get 'personaName' and 'age'.
                String[] lineParts = line.split(" ");

                //Compare this line personName with the one provided
                if(lineParts[0].equals(personName)) {
                    //Print age
                    System.out.println(lineParts[1]);
                    br.close();
                    System.exit(0);
                }
            }

            br.close();
            //If we got here, it means that personName was not found in the file.
            System.out.println("that name doesn't exists");
        } catch (IOException e) {
            System.out.println(" there is an IO Error");
        }
    }
}
于 2012-04-18T17:20:54.910 回答
0

使用 commons-io 并且不要忘记编码!

List<String> lines = FileUtils.readLines(file, encoding)
于 2012-07-24T07:17:13.567 回答
0

如果你使用Scanner类,它会让你的生活变得更轻松。

  Scanner fileScanner = new Scanner (new File(fileName));

  while(fileScanner.hasNextLine()
   {
      String line = fileScanner.nextLine();

      Scanner lineScanner = new Scanner(line);

      String name = lineScanner.next(); // gets the name
      double age  = Double.parseDouble(lineScanner.next()); // gets the age

      // That's all really! Now do the rest!
   }
于 2012-04-18T17:20:24.483 回答