2015-09-27 50 views
0

我想存儲從文本文件2個東西(只有名稱和值)東西后如何解析文本以分隔符開始whitespace-tilde-whitespace「〜」?

〜唱歌應該是一個線程被稱爲「名稱」一根弦和任何經過「:」冒號應該是一個整數稱爲「價值」

樣本輸出應該像
「NAME =」 公共無效結束() 「VALUE =」 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" ; 
      } 

     } 
+0

這肯定聽起來像一個XY問題。你真的想做什麼? – Tunaki

+0

我只想將文本文件中的數據存儲到名爲「name」的變量(gr.spinellis.ckjm.ClassMetrics getMetrics():)和值(1)變量名爲「Value」的變量中,併爲所有變量數據在文本文件裏面。 @Tunaki – mdsavs

回答

1
// 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(); 
} 
+0

謝謝你做這個訣竅;) – mdsavs

0

使用的代碼,我覺得你的問題是你分割字符串的方式。試試這個:

public static void main(String[] args) { 
    String input = "~ public void end(): 5"; 
    String[] split = input.split(":"); 

    System.out.println(split[0].substring(split[0].indexOf("~") + 1).trim()); 
    System.out.println(split[1].trim()); 
} 
+0

這個工作也謝謝 – mdsavs

0

這裏是你的代碼:

public class RegexTest { 
public static void main(String args[]) 
{ 
    String str = "~ public void end(): 5"; 

    String[] arr1 = str.split("~ "); 

    String[] arr2 = arr1[1].split(": "); 

    String name = arr2[0], value = arr2[1]; 

    System.out.println("Name: " + name + ", Value: " + value); 
} 
} 
+0

使用'readLine()'方法得到這一行後使用此代碼。 –