繁体   English   中英

Java中如何从多个txt文件(50个文件)中读取带有特殊字符的特定行

[英]How to read a specific line with special characters from multiple txt files (50 files) in Java

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

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

1.txt

hash

hash 如何运行 JavaScript

hash

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

2.txt

hash

hash 如何在 Windows 上安装 EclipseIDE 10

hash

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

3.txt

hash

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

hash

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

我要的output

如何运行 JavaScript

Windows上如何安装EclipseIDE 10

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

我试过的代码...

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();
                    }
                }
            }
        }
        
    }
}

读取每个文件的第二行:

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);
        }
    }
}

新路径 class 可以包含多个文件,例如 zip 中的文件,或 web 中的某个文件。

class 文件有许多不错的实用程序。

    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 送所有线路。

以下代码将起作用。

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("@", ""); - 用实际文件中的特殊字符替换@

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM