我是新來的java文本解析,我想知道什麼是解析文件時,每行的格式是已知的最佳方式。用C風格解析Java?
我有了每行以下格式的文件:
詮釋;字符串,雙,字符串,雙,字符串,雙,字符串,雙,字符串,雙
說明如何使用String ,雙作爲由逗號分隔的一對,每對用分號分隔。
舉幾個例子:
1;art,0.1;computer,0.5;programming,0.6;java,0.7;unix,0.3 2;291,0.8;database,0.6;computer,0.2;java,0.9;undegraduate,0.7 3;coffee,0.5;colombia,0.2;java,0.1;export,0.4;import,0.5
我用下面的代碼讀取每個行:
public static void main(String args[]) {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println(strLine);
}
// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
感謝提前:)