Home > Enterprise >  How to read a specific line with special characters from multiple txt files (50 files) in Java
How to read a specific line with special characters from multiple txt files (50 files) in Java

Time:10-01

Let's say we have 50 txt files with similar format and we want the 2nd line from each txt file. We want only the special character i.e. hash to get removed from the end result. Mentioning the demo content of only 3 files from the 50 files...

Note: I have special character hash in the files, which I can't mention here. Therefore, mentioning hash txt here.

1.txt

hash

hash How to run JavaScript

hash

2.txt

hash

hash How to install EclipseIDE on Windows 10

hash

3.txt

hash

hash NetBeans - How to increase the font size of editor, output and menu

hash

The output I want

How to run JavaScript

How to install EclipseIDE on Windows 10

NetBeans - How to increase the font size of editor, output and menu

The code I have tried...

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

CodePudding user response:

To read the second line of each file:

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

CodePudding user response:

The new Path class can contain more than only a File, for instance a file inside a zip, or some file in the web.

The class Files has many nice utilities.

    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 closes in every case, here in.

The Stream delivers all lines.

CodePudding user response:

The following code will work.

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("@", ""); - replace @ with the special character in your actual file.

  • Related