Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("rates.txt")));
for (int i=0; i<9; i++){
while(s.hasNext()){rates[i] = s.next();}
System.out.println(rates[i]);
}
}catch (IOException e){
System.out.println(e);
}
finally {
if (s != null) {
s.close();
}
}
當我運行此代碼時,它讀取我的txt文件中的最後一個字符塊,將它們置於rate [0]中,在1-9中保持null。我不確定爲什麼它首先讀取我的文件的末尾。 txt的內容如下..我無法使用FileReader將一個txt文件寫入數組(Java),我在做什麼錯了?
USD 1.34
EUR 1.00
JPY 126.28
GBP 0.88
INR 60.20
它讀取60.20,這是它正在記錄在陣列中。任何幫助,將不勝感激。我想我可以給你運行該代碼的結果:
run:
60.20
null
null
null
null
null
null
null
null
BUILD SUCCESSFUL (total time: 0 seconds)
你想要怎麼處理文本文件中的數據? – Finbarr 2010-04-10 13:19:35
如何在您的代碼中定義「rates」? – 2010-04-10 13:22:00