有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    比如:

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

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


    编辑评论:

    打印和读取文件内容时,您的工作过于复杂。不要使用ScannerBufferedReader。有人会替你做这项工作

    您的文件有非常特定的格式。您确实不需要为此目的使用regex,您正在内部while循环中使用它

    要使sample name匹配,请使用String#equales()方法并执行相应的操作

    1. 从文件的上半部分获取所需的值,如Sample NameRecording Time,将它们放在手边,以便以后使用

    2. 从下面的部分中,从每一行获取Peak Nameamount

    3. 打印时,通过使用这些收集的值来构造String


    另一个评论编辑:

    以下代码未经测试,因此可能存在一些问题,但您可以解决这些问题。 如果你看一下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);
    }
    

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