2012-04-18 97 views
1

即时学习我的编程期末考试。我必须编写一个程序,该程序打开存储在字符串fileName中的文件,并在文件中查找名为personName的字符串,并且这应该在personName之后打印第一个字符串,然后程序应该在打印后终止,如果参数personName为 是不是在文件中,那么它应该打印“这个名字不存在”,那么如果发生IOException,它应该然后打印“存在IO错误”并且程序应该存在使用system.exit(0)从java文本文件中读取字符串

程序应使用文件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

任何帮助,将不胜感激!

在此先感谢!

+1

好了,问题描述很清楚,但是您遇到的* specific *问题是什么? – amit 2012-04-18 16:59:47

+0

当我打印它时,我得到堆栈溢出错误,并且我没有得到打印的年龄 – 2012-04-18 17:01:58

+0

请附上整个堆栈跟踪和您得到的确切错误。 – amit 2012-04-18 17:04:02

回答

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"); 
     } 
    } 
} 
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! 
    } 
0

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

List<String> lines = FileUtils.readLines(file, encoding)