2015-02-11 73 views
0

我遇到了將文本文件拆分並從文本文件獲取信息到數組的問題,以便我可以選擇要打印的索引。 文本文件的格式是這樣的:嘗試從文本文件獲取信息並將信息拆分爲數組

X;25 
Y;15 

我想做的是分割文件,這樣我可以打印X 25和Y 15,如果我能得到它打印只單獨的一切:X, 25, Y, 15

這是我的代碼,我嘗試了很多不同的變體,但現在我完全卡住了。我已經被告知HashMap,但不知道該怎麼做。 任何能解決我的問題的人?

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile))); 

StringBuilder SB = new StringBuilder(); 
String assign = BR.readLine(); 

while((assign = BR.readLine()) != null) { 
    if (assign.contains(";")) { 
     SB.append(assign); 
     String assignment = SB.toString(); 
     String[] splitting = assignment.split(";"); 

     for (String s : splitting) { 
      System.out.println(s); 
     } 
    } 
} 
+0

如果你的格式是'X; 25Y; 15',那你爲什麼要用';'分割?結果會是'X,25Y,25'?但第一個問題應該是:爲什麼'X'的值和字母''Y''之間沒有分隔符?而且你知道你忽略了文件的第一行,對吧? – Tom 2015-02-11 08:34:07

+0

那就是如何格式化文本文件。我不能改變文本文件。我在哪裏以及爲什麼忽略第一行。 Cus是我打印的唯一東西是Y 15 – hjem 2015-02-11 08:38:28

+0

這裏:'String assign = BR.readLine();'。你讀了第一行,並沒有採取任何措施。你的最後一句話聽起來像你的文件格式不是'X; 25Y; 15',更可能是'X; 25 \ nY; 15'。所以請更新您的問題並提供適當的信息。例如,提供您正在嘗試閱讀和評估的文本文件的一部分。 – Tom 2015-02-11 08:41:10

回答

0

這應該工作:

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile))); 

String assign; 

while((assign = BR.readLine()) != null){ 

    String[] splitting= assign.split(";"); 

    for(int i =0; i < splitting.length; i++) { 
     System.out.println(splitting[i]); 
    } 

} 
0

爲什麼整個StringBuilder併發症? (未測試)

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile))); 

     String assign = BR.readLine(); //skips first line (if you have a header) 

     while((assign = BR.readLine()) != null){ 

      String[] splitting= assign.split(";"); 

      for(int i =0; i < splitting.length; i++) { 
       System.out.println(splitting[i]); 
      } 

     } 
+0

只能得到[Ljava.lang.String; @ 15db9742 [Ljava.lang.String; @ 15db9742打印出 – hjem 2015-02-11 08:39:28

+0

用System.out.println(splitting); – hjem 2015-02-11 08:39:49

+0

@hjem use'System.out.println(splitting [i]);' – Tom 2015-02-11 08:42:39

0

如果你喜歡檢索文件(而不僅僅是打印)的內容,那麼你可以嘗試下面的代碼。我使用Map來存儲文件內容並在稍後打印。

private static final String DELIMITER = ";"; // define a constant for the used delimiter 

public static void main(String args[]) throws Exception { 
    final Map<String, Integer> fileContent = readFile("test.txt"); 
    System.out.println(fileContent); 
} 

public static Map<String, Integer> readFile(final String fileName) { 
    final Map<String, Integer> content = new HashMap<>(); // define a map for the file content 
    // try-with-resources statement so the file will be closed safely 
    try (final FileReader fileReader = new FileReader(new File(fileName)); 
     final BufferedReader bufferedReader = new BufferedReader(fileReader)) { 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      if (line.contains(DELIMITER)) { 
       final String[] parts = line.split(DELIMITER); 
       // the second value is an integer, so we need to convert it 
       content.put(parts[0], Integer.parseInt(parts[1])); 
      } 
     } 
    } catch (IOException e) { 
     // handle exception a bit more, if you like ... 
     e.printStackTrace(); 
    } 
    return content; 
} 

的輸出將是:

{X=25, Y=15} 

這意味着在地圖fileContent包含鍵"X""Y"和它們的值是2515。如果您想獲得"X"的值,請使用int valueX = fileContent.get("X");

您當前的代碼不會關閉打開的文件閱讀器,這就是爲什麼我已將代碼包裝在try-with-resources statement中。它會爲我處理。

如果發生IOException(找不到文件,或文件無法讀取,或類似的東西),那麼我現在只是打印堆棧跟蹤。如果您喜歡:tutorial,您可以以不同的方式處理此案例。

試着玩上面的代碼,看看它是如何工作的。我希望它能幫助你。