簡體   English   中英

Java - 從文件中讀取結構松散的數據

[英]Java - Read loosely structured data from file

我目前正在嘗試了解 Java 代碼的行為方式以及如何處理輸入和輸出文件。 盡管我了解如何逐行讀取文件的內容並將它們放入數組中,但我很難理解如何從文件中讀取每 n 行出現的某些值,然后將它們放入數組中. 例如,我有一個如下所示的輸入測試文件:

2
Australia
John
42
Blue
USA
Jeremmy
15
Black

第一行是數組的大小。 以下幾行是我想從文件中讀取並放入所述數組的內容(在本例中,我將其設為:國家、姓名、年齡、眼睛顏色)。 換句話說,我想讀取一些每四行出現一次的對象屬性,以便稍后我可以在選擇時將它們打印出來,例如其中一個人。 現在我被困在這里,不知道如何前進,因為大多數人沒有嘗試對這樣的文件進行操作。

   private static String[] readPeople(File file) {
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String sizeText = br.readLine();
        int size = Integer.parseInt(sizeText);
        String[] peopleSet= new String[size];

        for (int i = 1; i < size; i++) {
            if (i % 2 != 0) {
                String peopleInfo= br.readLine();
                peopleSet[i] = peopleInfo;
            }
        }
        return peopleSet;
    } catch (IOException ex) {
        System.err.println(ex);
        return new String[0];
    }
}

使行冒號分隔可能更容易

您的條目是否以冒號分隔在文件中的值之間:

Australia:John:42:Blue
USA:Jeremmy:15:Black

然后在您的文件解析器中:

private static List<Person> readPeople(File file) {
    List<Person> people = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
      String line = "";
      while((line = br.readLine()) != null) {
        String[] args = line.split(":");
        String country = args[0];
        String name = args[1];
        int age = Integer.parseInt(args[2]);
        String eyeColor = args[3];

        Person p = new Person(name, country, age, eyeColor);
        people.add(p);
      }
    } catch (IOException ex) {
      System.err.println(ex);
    } 
    return people;
}

最后定義 Person 類

class Person {
   String name;
   String country;
   int age;
   String eyeColor;

   public Person(String name, String country, int age, String eyeColor) {
      this.name = name;
      this.country = country;
      this.age = age;
      this.eyeColor = eyeColor;
   }

   @Override
   public String toString() {
     return String.format("%s:%s:%d:%s", country, name, age, eyeColor);
   }
}

根據需要添加錯誤檢查和 getter/setter

這將返回文件中定義的人員列表,您可以通過在返回列表對象上調用.size()來檢查列表的大小。

更新

添加toString()覆蓋以創建冒號單獨條目,以便將其寫出到文件或控制台

希望這可以幫助

我給你這個代碼。

    File Read_File = new File("file_path"); 

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(Read_File));
        String line;
        int line_number = 0;

        int size = Integer.parseInt(br.readLine());



        for (int try_ = 0; try_ < size; try_++) {
            String read = br.readLine();
            for(int try_four = 0; try_four<4; try_four++) {
                //save input operation
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null)
            try {
                br.close();
            } catch (IOException e) {
            }
    } 

但使用列表是最好的方法。

問題是您的代碼僅在條件為真時從文件中讀取。 無論您循環多少次,文件內容僅在您實際使用br.readLine()閱讀時才會前進。

for (int i = 1; i < size; i++) {
    if (i % 2 != 0) {
        String peopleInfo= br.readLine();
        peopleSet[i] = peopleInfo;
    }
}

也不清楚你想讀哪幾行。 這段代碼回路size次(2),但只讀過線隔日一次,因此它只會讀“澳大利亞”。

如果您想讀取每個四人組中的第三行的人名(“John”和“Jeremmy”),您可以這樣做:

for (int i = 1; i < size; i++) {
    for (int j = 0; j < 4; j++) {
        String line = br.readLine();
        if (j % 4 != 2) {
            people[i] = line;
        }
    }
}

這讀取所有八行(從“澳大利亞”到“黑色”)。 對於每個人, j的值將從03 因此,第三行是當j % 4 == 2

它將名稱存儲到長度為 2 的數組people中。我從peopleSet重命名它,因為它不是 Java Set 原始名稱具有誤導性。

暫無
暫無

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

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