用C风格的Java解析?

发布于 2024-12-15 16:40:27 字数 1245 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〗斷ホ乔殘χμё〖 2024-12-22 16:40:27

您可以使用 扫描仪< /a> 类,对于初学者:

一个简单的文本扫描器,可以使用正则表达式解析原始类型和字符串。

You could use the Scanner class, for starters:

A simple text scanner which can parse primitive types and strings using regular expressions.

江南月 2024-12-22 16:40:27

如果您确实尝试进行“C”样式解析,那么包含为“下一个”字段累积的字符的缓冲区在哪里?检查字段分隔符是否被读取的检查在哪里,一旦读取行尾/字段分隔符,将当前字段刷新到正确的数据结构的代码在哪里?

Java 中的一个字符一个字符的读取循环看起来像

int readChar = 0;
while ((readChar = in.read()) != -1) {
   // do something with the new readChar.
}

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

int readChar = 0;
while ((readChar = in.read()) != -1) {
   // do something with the new readChar.
}
守望孤独 2024-12-22 16:40:27

您可以提供一个模式并使用 扫描程序

String input = "fish1-1 fish2-2";
java.util.Scanner s = new java.util.Scanner(input);
s.findInLine("(\\d+)");
java.util.regex.MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
    System.out.println(result.group(i));
s.close(); 

You can provide a pattern and use the Scanner

String input = "fish1-1 fish2-2";
java.util.Scanner s = new java.util.Scanner(input);
s.findInLine("(\\d+)");
java.util.regex.MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
    System.out.println(result.group(i));
s.close(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文