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

谢谢!

最佳答案

类似于:

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

//Your code

您可以在内部 while 循环中对峰值名称金额值 的特定表项执行相同操作。在这种情况下,您可以使用 String#equales()方法。

<小时/>

编辑评论:

在打印和读取文件内容时,您的事情过于复杂化。不要使用 Scanner 以及 BufferedReader。有人将为您完成这项工作。

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

要使样本名称匹配,请使用String#equales()方法并进行相应的操作。

  1. 从文件的上半部分获取您需要的值,例如样本名称录音时间,并将它们放在手边,以便以后使用它们。

  2. 从下半部分获取每行的峰名称数量

  3. 在打印时,通过使用这些收集到的值构造您的字符串

<小时/>

评论的另一次编辑:

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

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

关于java - 读取文件并显示想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20334890/

相关文章:

java - Android 将 Java API 追溯到 jni

java - selenium webdriver xpath InvalidSelectorError

java - 共享实例变量与局部变量

java - 使用递归打印递增和递减的数字

java - 使用invokeLater

java - java解决方案中的动态类创建

java - 将元素添加到模拟列表

java - 查找一个图,该图有 6 个顶点,每个顶点都是一条边的终点或起点,并且代码返回数字 2

java - CMU Sphinx 是否可以通过 Maven 获得?

java - broadcastReceiver 无法根据通知工作(pendingIntent)