读取文件并显示想要的结果

时间:2013-12-02 18:06:20

标签: java

我有一个程序可以读取下面的文件。

12  9-62-1                      

Sample Name:        9-62-1          Injection Volume:       25.0  
Vial Number:        37          Channel:        ECD_1
Sample Type:        unknown         Wavelength:     n.a.
Control Program:        Anions Run          Bandwidth:      n.a.
Quantif. Method:        Anions Method           Dilution Factor:        1.0000  
Recording Time:     10/2/2013 19:55         Sample Weight:      1.0000  
Run Time (min):     14.00           Sample Amount:      1.0000  






No.     Ret.Time    Peak Name   Height  Area    Rel.Area    Amount  Type 
    min     µS  µS*min  %   mG/L    
1   2.99        Fluoride    7.341   1.989   0.87        10.458  BMB
2   3.88        Chloride    425.633     108.551     47.72       671.120     BMb
3   4.54        Nitrite 397.537     115.237     50.66       403.430     bMB
4   5.39        n.a.    0.470   0.140   0.06        n.a.    BMB
5   11.22       Sulfate 4.232   1.564   0.69        13.064  BMB
Total:          835.213     227.482     100.00      1098.073 

从这些文件中,程序应该输出一些东西而不是一切。 我需要的最终结果应如下所示:

0012.TXT
Sample#,Date,Time,Peak Name, Amount
9-62-1,10/2/2013,19:55,Fluoride,10.458
9-62-1,10/2/2013,19:55,Chloride,671.120
9-62-1,10/2/2013,19:55,Nitrite,403.430 
9-62-1,10/2/2013,19:55,Sulfate,13.064

但是,现在他们看起来像这样:

0012.TXT
Sample#,Date,Time,Peak Name, Amount
9-62-1,10/2/2013,19:55,Fluoride,10.458 ,
Chloride,671.120 ,
Nitrite,403.430 ,
n.a.,n.a.,
Sulfate,13.064 ,
,1098.073 ,

这是我的代码和我所做的。

Scanner input = new Scanner(new FileReader(selectFile.getSelectedFile()));
                    System.out.println("Sample#,Date,Time,Peak Name,Amount");

                    int linesToSkip = 28;

                    BufferedReader br = new BufferedReader(new FileReader(selectFile.getSelectedFile()));

                        String line;
                        while ( (line = br.readLine()) != null) {
                            if (linesToSkip-- > 0) {
                            continue;
                            }
                            if (line.contains("n.a.")) {
                            continue;
                            }
                            if (line.contains("Total")) { 
                                continue;
                            }

                            String[] values = line.split("\t");

                            int index = 0;
                            for (String value : values) {
                                /*System.out.println("values[" + index + "] = " + value);*/
                                index++;



                                }

                                while (input.hasNext()) {

                                        String word = input.next();
                                        Pattern pattern1 = Pattern.compile("Name:");
                                        Pattern pattern2 = Pattern.compile("Time:");
                                        Matcher matcher1 = pattern1.matcher(word);
                                        Matcher matcher2 = pattern2.matcher(word);
                                        Matcher matcher3 = pattern2.matcher(word);

                                        if(matcher1.matches()){
                                            System.out.print(input.next() + ",");
                                        }
                                        if(matcher2.matches()){
                                            System.out.print(input.next() + ",");
                                        }
                                        if(matcher3.matches()){
                                            System.out.print(input.next() + ",");
                                        } 
                                        System.out.print("");
                                }

                                System.out.print(values[2]+",");
                                System.out.println(values[6]+"\b,"); 


                        }
                   br.close();

如何使用样本#,日期和时间使输出看起来像这些,然后是峰值名称和数量,并在每一行打印出来?

Sample#,Date,Time,Peak Name, Amount
9-62-1,10/2/2013,19:55,Fluoride,10.458
9-62-1,10/2/2013,19:55,Chloride,671.120
9-62-1,10/2/2013,19:55,Nitrite,403.430 
9-62-1,10/2/2013,19:55,Sulfate,13.064

谢谢!

1 个答案:

答案 0 :(得分:2)

类似的东西:

while ( (line = br.readLine()) != null) {
     if (line.contains("n.a.")) {
       continue;
     }

//Your code

您可以在whilepeak name value的特定表项的内部amount value循环中执行相同的操作。在这种情况下,您可以使用String#equales()方法。


编辑评论:

在打印和阅读文件内容时,您的工作过于复杂。不要使用Scanner以及BufferedReader。一个人会为你做的工作。

您的文件格式非常具体。您实际上不需要使用regex来实现此目的,而是在内部while循环中使用它。

sample name匹配使用String#equales()方法,请相应地进行操作。

  1. 从文件的上半部分获取所需的值,例如Sample NameRecording Time,让它们保持方便,以便您以后可以使用它们。

  2. 从下方开始,每行获取Peak Nameamount

  3. 通过使用这些收集的值打印构建您的String


  4. 另一个评论编辑:

    以下代码未经过测试,因此可能存在一些问题,但您可以解决它们。 如果你看String Class,你会发现很多有用的方法。

    BufferedReader br = new BufferedReader(new FileReader(selectFile.getSelectedFile()));
    
    String recTime, peakName, amount, sample ;
    int linesToSkip = 28;
    String line = br.readLine();
    
    if(line != null){
        String[] values = line.split("\t");
        sample = values[1];
    }
    
    while ( (line = br.readLine()) != null) {
        values = line.split("\t");
    
        if (line.startsWith("Sample Name")) { 
           // Check here value[1] is equal to sample. If this is needed.
           // You got your sample name here 
        } else if (line.startsWith("Recording Time")) { 
           recTime = values[1];
           // You got your Recording Time here 
        } else if(values.length > 4 ){
           // get Peak Name and recording time
           peakName = values[2];
           amount = values[6];
        } else if (line.contains("n.a.") || line.contains("Total") || linesToSkip-- > 0) {
           /* may not needed linesToSkip-- > 0 in above condition */ 
           continue;
        } 
    
        System.out.println(sample +" ," + recTime + " ," + peakName + " ," + amount);
    }
    

    我希望这会有所帮助。祝你好运。