如何获得以下输出?

时间:2013-12-03 16:58:22

标签: 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    

我需要让它输出如下结果:

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,

基本上包括每个峰值名称和金额的相同样本#,日期和时间。

我目前的结果是这样的:

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

它打印第二行好,但对于其他行,它不会打印样本#,日期和时间。

我该如何解决这个问题? 谢谢!

这是我的代码:

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

                    int linesToSkip = 29;

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

                        String line; 
                        while ( (line = br.readLine()) != null) {
                            if (--linesToSkip > 0) {
                            continue;
                            }
                            if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
                            continue;
                            }
                            if (line.contains("n.a.")) {
                            continue;
                            }
                            if (line.contains("Total")) { 
                                continue;
                            }
                            if (line.startsWith("Name:")) {System.out.print(input.next());}

                            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(values[2]+",");
                                System.out.println(values[6]+"\b,");




                        }
                   br.close();                    
                }

                catch(IOException e) 
                {               
                    System.out.println("The file cannot be read");
                }

1 个答案:

答案 0 :(得分:1)

我不得不对您的程序进行一些更改,我必须将文本文件选项卡分隔为您的程序所期望的。我希望这会对你有所帮助:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reader {

    private static String lineHolder;
    public static void main(String [] args) throws IOException{
        try  
        {  
            String sampleNamefile="/tmp/two.txt";
            /*Sample Name:        9-62-1          Injection Volume:       25.0
             */

            String noFile="/tmp/oneNew.txt";
            /*          No.     Ret.Time        Peak Name       Height  Area    Rel.Area        Amount  Type
            min     µS      µS*min  %       mG/L

             */         

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

            int linesToSkip = 29;

            File outputFile2 = new File(noFile);

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

                String line; 
                while ( (line = br.readLine()) != null) {
                  /*  if (--linesToSkip > 0) {
                    continue;
                    }*/
                    if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
                    continue;
                    }
                    if (line.contains("n.a.")) {
                    continue;
                    }
                    if (line.contains("Total")) { 
                        continue;
                    }
                    if (line.startsWith("Name:")) {System.out.print(input.next());}

                    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() + ",");
                    lineHolder=input.next()+ "," ;
                    }
                    if(matcher2.matches()){
                    //System.out.print(input.next() + ",");
                    lineHolder+=input.next() + ",";
                    }
                    if(matcher3.matches()){
                   // System.out.print(input.next() + ",");
                    lineHolder+=input.next()+ "," ;
                    }
                    }
                    if(!line.contains("No.  ") ){
                        if(!line.contains("min") ){
                             System.out.print(lineHolder);
                        System.out.print(values[2]+",");
                       System.out.println(values[6]+"\b,");
                        }
                    }
                }
           br.close(); 

    }finally{

    }

    }


}

结果如下: 样品#,日期,时间,峰名,金额

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,