簡體   English   中英

從文本文件中讀取Java

[英]Java read from a text file

我正在嘗試使用掃描儀將一些數據從文本文件加載到GUI。 我的文本文件中有兩個部分:俱樂部和會員。 該代碼在“俱樂部”部分運行正常。 例如,如果我的列表中有4個俱樂部,則將全部顯示,但對於“成員”部分而言,列表中有多少成員並不重要,只會顯示第一個成員。 這是我的代碼:

public void load (String fileName) throws FileNotFoundException {
     FileInputStream fileIn = new FileInputStream("Clubs.txt");
     Scanner scan = new Scanner(fileIn);

     while (scan.hasNextLine()){
            String line = scan.nextLine();
            if(line.equals("Members")){
                String firstName = scan.next();
                String lastName = scan.next();
                Pupil p1 = new Pupil( firstName, lastName);
                pupils[nbrPupils] = p1;
                nbrPupils ++;

               }   
             else if(line.equals("Clubs")){ 
                 while (scan.hasNext()){
                 String club = scan.nextLine();
                 Club aNewClub = new Club(club);
                 clubs[nbrClubs] = aNewClub;
                 nbrClubs ++;

                }

        }

提示:您正在“ Clubs部分中進行while (scan.hasNext()) ,但在“ Members部分中未進行。

while循環轉換為if條件,因為您只想檢查是否存在下一行

else if (line.equals("Clubs")) {
       if (scan.hasNext()) {/////here if you use while loop , it will loop until the file is finish 
         String club = scan.nextLine();
        }
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        new ReadFile().load("");
    }

    public void load (String fileName) throws FileNotFoundException {
         FileInputStream fileIn = new FileInputStream("C:\\Clubs.txt");
         Scanner scan = new Scanner(fileIn);
         boolean membersfound =false;
         while (scan.hasNextLine()){
                String line = scan.nextLine();

                if(line.equals("Members") || membersfound){

                    String firstName = scan.next();
                    String lastName = scan.next();
                   System.out.println("Member   "+firstName +":"+ lastName);

                   }   
                 else if(line.equals("Clubs") ){ 
                     while (scan.hasNext()){
                     String club = scan.nextLine();
                     if( club.equals("Members")){
                         membersfound = true;
                         break;
                     }
                     System.out.println("Clubname :" + club
                             );                

                    }

            }
}
    }
}

我本可以修改整個程序。 但我想向您展示您的錯誤樣本clubs.txt文件成員member1 member2 member3 Clubs club1 club2 club3

當您執行scan.nextLine()它將掃描儀移至當前讀入的行之后的行。因此,如果繼續執行scan.next() ,掃描儀將從當前行的末尾開始(在本例中為line ),然后閱讀后續內容。

看到這里說:

“使該掃描儀前進到當前行之后,並返回跳過的輸入

你可以只為調用split()substring()line和提取所需的信息。

暫無
暫無

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

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