【发布时间】:2011-11-16 19:43:01
【问题描述】:
我是 java 文本解析的新手,我想知道当每行的格式已知时解析文件的最佳方法是什么。
我有一个文件,每一行的格式如下:
Int;String,double;String,double;String,double;String,double;String,double
注意 String,double 如何充当以逗号分隔的一对,每对由分号分隔。
几个例子:
1;艺术,0.1;计算机,0.5;编程,0.6;java,0.7;unix,0.3 2;291,0.8;数据库,0.6;计算机,0.2;java,0.9;本科,0.7 3;咖啡,0.5;哥伦比亚,0.2;java,0.1;出口,0.4;进口,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());
}
}
提前致谢:)
【问题讨论】:
标签: java text-parsing line-processing