【问题标题】:Parsing in Java with C style?用 C 风格在 Java 中解析?
【发布时间】: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


    【解决方案1】:

    对于初学者,您可以使用 Scanner 类:

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

    【讨论】:

      【解决方案2】:

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

      Java 中逐字符读取循环的样子

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

      【讨论】:

        【解决方案3】:

        您可以提供一个模式并使用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(); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-18
          • 2014-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-11
          • 1970-01-01
          • 2012-11-25
          相关资源
          最近更新 更多