2012-04-18 103 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)