用C风格的Java解析?
我是java文本解析的新手,我想知道当每行的格式已知时解析文件的最佳方法是什么。
我有一个文件,每行具有以下格式:
Int;String,double;String,double;String,double;String,double;String,double
请注意 String,double 如何充当由逗号分隔的一对,每个对由分号分隔。
几个例子:
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());
}
}
提前致谢:)
I am new to java text parsing and I'm wondering what is the best way to parse a file when the format of each line is known.
I have a file that has the following format for each line:
Int;String,double;String,double;String,double;String,double;String,double
Note how the String,double act as a pair separated by a comma and each pair is separated by a semicolon.
A few examples:
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
I'm using the following code to read each line:
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());
}
}
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
扫描仪
< /a> 类,对于初学者:You could use the
Scanner
class, for starters:如果您确实尝试进行“C”样式解析,那么包含为“下一个”字段累积的字符的缓冲区在哪里?检查字段分隔符是否被读取的检查在哪里,一旦读取行尾/字段分隔符,将当前字段刷新到正确的数据结构的代码在哪里?
Java 中的一个字符一个字符的读取循环看起来像
If you are truly trying to do "C" style parsing, where is the buffer which contains the characters being accumulated for the "next" field? Where is the check that sees if the field separator was read, and where is the code that flushes the current field into the correct data structure once the end of line / field separator is read?
A character by character read loop in Java looks like
您可以提供一个模式并使用 扫描程序
You can provide a pattern and use the Scanner