Home > Net >  I want to print all the log lines in a file so please help me with the Java program so that I can al
I want to print all the log lines in a file so please help me with the Java program so that I can al

Time:12-06

So please help me with the program where I can create one new file and write into file and then print all the Log lines when used multiple methods in java program. So when those multiple method contain multiple lines I want to print all those lines into one file where all the log lines are printed one after the other sequentially.

CodePudding user response:

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

public class Main {
  public static void main(String[] args) {
    // Check if a filename was provided as an argument
    if (args.length != 1) {
      System.out.println("Usage: java PrintFile <filename>");
      return;
    }

    // The filename is the first argument
    String filename = args[0];

    // Read the file and print each line
    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException e) {
      System.out.println("Error reading file: "   e.getMessage());
    }
  }
}

   

then run java PrintFile filename.txt

CodePudding user response:

If you use Log4j with spring boot on your project, you can do that following this tutorial.

https://www.baeldung.com/spring-boot-logging

  • Related