【问题标题】:How to read a specific line with special characters from multiple txt files (50 files) in Java如何从 Java 中的多个 txt 文件(50 个文件)中读取带有特殊字符的特定行
【发布时间】:2021-10-01 14:28:24
【问题描述】:

假设我们有 50 个格式相似的 txt 文件,我们想要每个 txt 文件的第二行。我们只希望从最终结果中删除特殊字符,即哈希。 提到50个文件中只有3个文件的demo内容...

注意:我在文件中有特殊字符哈希,这里我不能提及。因此,在这里提到 hash txt。

1.txt

散列

hash 如何运行 JavaScript

散列

这里的问题的答案......可以是任意数量的行

2.txt

散列

hash 如何在 Windows 10 上安装 EclipseIDE

散列

这里的问题的答案......可以是任意数量的行

3.txt

散列

hash NetBeans - 如何增加编辑器、输出和菜单的字体大小

散列

这里的问题的答案......可以是任意数量的行

我想要的输出

如何运行 JavaScript

如何在 Windows 10 上安装 EclipseIDE

NetBeans - 如何增加编辑器、输出和菜单的字体大小

我试过的代码...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException{  
        File[] files = {new File("1.txt"), new File("2.txt")};
        // Fetching all the files
        for (File file : files) {
            if(file.isFile()) {
                BufferedReader inputStream = null;
                String line;
                try {
                    inputStream = new BufferedReader(new FileReader(file));
                    while ((myline = inputStream.readLine()) != null) {
                        System.out.println(myline);
                    }
                }catch(IOException e) {
                    System.out.println(e);
                }
                finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            }
        }
        
    }
}

【问题讨论】:

  • 我没有遇到您真正的问题,但您可以使用 if( myline.contains("hash") ) , if( myline.endsWith("hash)) , if( myline.startsWith("hash)) 或许多其他方法和字符串上的模式来验证单词以检查,您可以在打印 myline.replace("hash","") 之前替换该行
  • 好吧,readLine 给你一行。所以可以读第一行,把它扔掉,然后再读第二行(检查两者都不为空)。然后你有第二行的字符串,你可以删除哈希。如果该哈希具有固定长度,则只需使用String.substring(int)。否则,您可能必须使用正则表达式,具体取决于“哈希”模式的复杂程度。

标签: java file bufferedreader filereader


【解决方案1】:

读取每个文件的第二行:

String hash = "...";

for (File file : files) {
    if (file.isFile()) {
        try (BufferedReader reader =
            new BufferedReader(new FileReader(file))) {

            // Skip first line.
            reader.readLine();

            // Read second line.
            String line = reader.readLine();

            // Remove hash from start of line
            line = line.substring(hash.length());

            System.out.println(line);
        }
    }
}

【讨论】:

    【解决方案2】:

    新的 Path 类不仅可以包含 File,例如 zip 中的文件或 Web 中的某些文件。

    Files 类有很多不错的实用程序。

        Path[] files = {Paths.get("1.txt"), Paths.get("2.txt")};
        for (Path file : files) {
            try (Stream<String> in = Files.lines(Charset.defaultCharset())) {
                in.skip(1) // Skip first line.
                  .forEach(line -> System.out.println(line.replace("hash", ""));
            }
        }
    

    try-with-resources 在任何情况下都会关闭,这里是in

    Stream 提供所有线路。

    【讨论】:

      【解决方案3】:

      以下代码将起作用。

      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      
      public class Splitter {
          public static void main(String[] args) throws IOException {
              File[] files = {new File("1.txt"), new File("2.txt")};
              // Fetching all the files
              for (File file : files) {
                  if(file.isFile()) {
                      BufferedReader inputStream = null;
                      String line;
                      try {
                          inputStream = new BufferedReader(new FileReader(file));
                          while ((line = inputStream.readLine()) != null) {
                              line = line.replaceAll("@", "");
                              System.out.println(line);
                          }
                      }catch(IOException e) {
                          System.out.println(e);
                      }
                      finally {
                          if (inputStream != null) {
                              inputStream.close();
                          }
                      }
                  }
              }
      
          }
      }
      

      line = line.replaceAll("@", ""); - 将@ 替换为您实际文件中的特殊字符。

      【讨论】:

      • 从问题的第一句开始:“我们想要每个 txt 文件的第二行。”
      • 每个文件的第二行是:如何运行 JavaScript、如何在 Windows 10 上安装 EclipseIDE 和 NetBeans - 如何增加编辑器、输出和菜单的字体大小。 OP提到第一行和第三行有一个特殊字符(用哈希代替)。第二行还包含一个特殊字符。
      • @Asif 是的,第二行还包含一个特殊字符。但是,我只想要每个文本文件的第二行。另外,我想添加 50+ txt 文件,它们位于同一目录中。显然,我不会提及代码中的所有文件名。我也想传递完整的目录。此外,您的代码不只显示第二行。
      • @Aman - 感谢您的澄清!为误导的答案道歉。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2017-10-02
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多