java - 如何解析以分隔符whitespace-tilde-whitespace "~ "开头的文本?

标签 java parsing

我想从文本文件存储 2 个东西(仅名称和值)

~sing 之后的任何内容都应该作为一个名为“Name”的字符串进行线程处理,而“:”冒号之后的任何内容都应该是一个名为“Value”的整数

示例输出应如下所示
“名称=” public void end() “值=” 5 ...等等

文本文件的内容

    ~ public void end(): 5

    ~ public void <init>(org.apache.bcel.classfile.JavaClass arg0, gr.spinellis.ckjm.ClassMetricsContainer arg1): 1

    ~ public gr.spinellis.ckjm.ClassMetrics getMetrics(): 1

    ~ private void incRFC(String arg0, String arg1, org.apache.bcel.generic.Type[] arg2): 1

    ~ public void start(): 1

    ~ void registerMethodInvocation(String arg0, String arg1, org.apache.bcel.generic.Type[] arg2): 1

    ~ public void visitField(org.apache.bcel.classfile.Field arg0): 1

    ~ public void visitJavaClass(org.apache.bcel.classfile.JavaClass arg0): 5

    ~ void registerFieldAccess(String arg0, String arg1): 2

    ~ static String className(org.apache.bcel.generic.Type arg0): 3

    ~ public void registerCoupling(org.apache.bcel.generic.Type arg0): 1

    ~ void <init>(): 1

    ~ public gr.spinellis.ckjm.ClassMetrics getMetrics(String arg0): 2

    ~ public void printMetrics(gr.spinellis.ckjm.CkjmOutputHandler arg0): 3

这是我尝试在标题问题中使用的代码

        package javaapplication38;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.nio.charset.StandardCharsets;
        import java.nio.file.Files;
        import java.nio.file.Path;
        import java.nio.file.Paths;
        import java.util.ArrayList;
        import java.util.List;

        /*  to read CSV file in Java. In this program we will read * list of metrics  stored in CSV file as comma separated values. */
        public class readingtry {


            public static void main(String... args) {
                List<Metrics> metric = readMetricFromCSV("C:\\Users\\hp\\Desktop\\alamah.txt");

                // let's print all the metric read from CSV file
                for (Metrics m : metric) {
                    System.out.println(m);
                }
            }

            private static List<Metrics> readMetricFromCSV(String fileName) {
                List<Metrics> metricsss = new ArrayList<>();
                Path pathToFile = Paths.get(fileName);

                // create an instance of BufferedReader
                // using try with resource, Java 7 feature to close resources
                try (BufferedReader br = Files.newBufferedReader(pathToFile,
                        StandardCharsets.US_ASCII)) {

                    // read the first line from the text file
                    String line = br.readLine();

                    // loop until all lines are read
                    while (line != null) {


                    String[] attributes = line.split(" ");  // the file, using a comma as the delimiter
                    Metrics valueOfMetric = createMetric(attributes);
                    metricsss.add(valueOfMetric);      // adding metric  into ArrayList
                    line = br.readLine();



                        // use string.split to load a string array with the values from
                        // each line of
                        // the file, using a comma as the delimiter
                        String[] attributes = line.split(" ~ ");

                        Metrics valueOfMetric = createMetric(attributes);

                        // adding book into ArrayList
                        metricsss.add(valueOfMetric);

                        // read next line before looping
                                         line = br.readLine();
                    }


                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
                return metricsss;
            }

            private static Metrics createMetric(String[] metadata) {
                //classname classvalue  
                String name=  (metadata[0]);
                  int value = Integer.parseInt(metadata[1 ]);


               //                                
                return new Metrics(name,value);


            }

        }

        class Metrics {
        private String name;
        private int value;


            public Metrics( String name,int value) {
                this.name = name;
                  this.value=value;

            }

            public String getName() {        return name;    }
            public void setName(String name) {        this.name = name;    }

             public int getvalue() {        return value;    }
            public void setvalue(int value) {        this.value = value;    }


            @Override
            public String toString() {
                return "name=  " + name +"value =  " + value+"\n\n" ;
            }

        }

最佳答案

// read the first line from the text file
String line = br.readLine();

// loop until all lines are read
while (line != null) {

    // use string.split to load a string array with the values from
    // each line of
    // the file, using a comma as the delimiter
    String[] attributes = line.split(" ~ ");
    if (attributes.length == 2) {
        String[] nameValue = attributes[1].split(": ");
        if (nameValue.length == 2) {
            Metrics valueOfMetric = createMetric(nameValue);
            // adding book into ArrayList
            metricsss.add(valueOfMetric);
        }
    }

    // read next line before looping
    line = br.readLine();
}

关于java - 如何解析以分隔符whitespace-tilde-whitespace "~ "开头的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810685/

相关文章:

java - iText7 setValue 方法不起作用

java - 如何使用 Maven 构建 hello world 类?

c# - 这个日期可以自动转换吗?

c - 在不求助于 GLR-Parser 的情况下解决模棱两可的语法

java - 基于正则表达式用Java添加html标签,保持数据匹配

java - Jcombobox,当选择某个项目时,显示一个值

java - Java 文件未找到异常

java - 如何通过 selenium-webdriver 和 Java 使用 java 进行鼠标悬停

php - 单元格停止行处理中的未转义字符 PHPExcel

parsing - 使用 awk 在 $4 和 $5 之间添加一列