我遇到的問題是將簡單文本文件的每一行插入到我想要的特定變量中。例如我的文本文件是有序像這樣(在字符串之間的文本文件中沒有空間只有這個編輯器就會把它們放在一起)如何將文本讀取字符串插入到特定變量
達拉斯
78˚F
北,15英里每小時
dallasimage
丹佛
29女性
南,10英里每小時
denverimage
,我想達拉斯市的變量,78F的溫度變化,並依此類推,直到文本結束,此刻是所有進入城市變量,它所有打印從那裏!我知道掃描儀有一個下一行,但從來沒有使用過,所以我被困在這裏。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Textreader {
public static void main(String[] args) {
try {
String city ="a";
String temp ="b";
String wind ="c";
String image = "d";
File file = new File("load.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
city = stringBuffer.append(line).toString();
stringBuffer.append("\n");
//city = line.toString();
temp = stringBuffer.append(line).toString();
wind = line;
image = line;
//stringBuffer.append(line);
//stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(city);
System.out.println(temp);
//System.out.println(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
我這樣做,而是它確實增量,但它保持的一切讀取前一行幷包括它。例如,對於[1]行,它讀達拉斯,78 f,行[2]它讀達拉斯,78F北,15英里 – user3034262 2014-12-06 06:29:57
嗨,我的壞。這件事太快了。我更正了代碼。這是一個簡單的例子,並不是解決問題的最佳方法。不過,它會爲你做這項工作。希望這可以幫助。 – Apokai 2014-12-06 23:27:06