簡體   English   中英

從Java中的文本文件讀取字符串

[英]Reading Strings from text files in java

我正在為我的編程期末考試而學習。 我必須編寫一個程序來打開一個存儲在字符串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

任何幫助將非常感激!!

提前致謝!

這就是我想您正在嘗試做的。 如果沒有,至少可以作為示例。 就像amit提到的那樣,您當前的錯誤是由於遞歸調用引起的,我認為這是不必要的。

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

如果您使用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!
   }

使用commons-io,不要忘記編碼!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM