Java reads the log from the specified path

ddwshow 注册会员
2023-02-27 14:04

Here is the Java code to combine the same name log files in two folders into a new txt file in date order:

import java.io.*;
import java.text.*;
import java.util.*;

public class MergeLogs {
    public static void main(String[] args) throws Exception {
        // 输入源文件夹路径和目标文件路径
        String sourceFolder1 = "path/to/source/folder1";
        String sourceFolder2 = "path/to/source/folder2";
        String destinationFile = "path/to/destination/file.txt";

        // 获取源文件夹1中所有的log文件
        File folder1 = new File(sourceFolder1);
        File[] listOfFiles1 = folder1.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".log");

        // 获取源文件夹2中所有的log文件
        File folder2 = new File(sourceFolder2);
        File[] listOfFiles2 = folder2.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".log");

        // 将两个文件夹中所有的log文件放在一个数组中
        File[] listOfFiles = new File[listOfFiles1.length + listOfFiles2.length];
        System.arraycopy(listOfFiles1, 0, listOfFiles, 0, listOfFiles1.length);
        System.arraycopy(listOfFiles2, 0, listOfFiles, listOfFiles1.length, listOfFiles2.length);

        // 对log文件按日期进行排序
        Arrays.sort(listOfFiles, new Comparator() {
            public int compare(File f1, File f2) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                try {
                    Date d1 = sdf.parse(f1.getName().substring(0, 8));
                    Date d2 = sdf.parse(f2.getName().substring(0, 8));
                    return d1.compareTo(d2);
                } catch (ParseException e) {
                    return 0;

        // 将log文件中的内容拼接到目标文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter(destinationFile));
        for (File file : listOfFiles) {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {

        System.out.println("Logs merged successfully!");
In the

code, you first need to enter the source folder 1, source folder 2, and destination file path. It then takes all the log files in source folders 1 and 2 through the File class and the FilenameFilter interface and puts them in an array. The log files are then sorted by date using the Arrays.sort method and the Comparator interface. Finally, the BufferedWriter class is used to concatenate the contents of the log file into the object file and close the file stream. The message "Logs merged successfully!" is displayed. .

dansiliu 注册会员
2023-02-27 14:04

This answer quotes ChatGPT

To combine the logs of the same name in two folders into a new txt document in date order, you can perform the following operations:

1. Obtain the log File list in both folders: Use Java's file class and listFiles() method to obtain the log file list in both folders. You can use FileFilter to filter out.log files.

2.sort by date: For the list of log files in each folder, you can sort by the date of the file name using the Collections.sort() method. You can use regular expressions to extract the Date information in the file name and convert the file name to the date type for sorting.

Read log files one by one and write new txt documents: For the sorted list of log files, read each log file one by one and write it to the new txt document. You can use Java's BufferedReader and BufferedWriter classes to read and write files.

Here is an example of Java code that does this:

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MergeLogFiles {

    public static void main(String[] args) throws Exception {
        // 定义两个log文件夹的路径
        String folderPath1 = "D:/logs1";
        String folderPath2 = "D:/logs2";
        // 定义新的txt文档路径
        String outputPath = "D:/merged_logs.txt";

        // 获取两个文件夹下的log文件列表
        List fileList = new ArrayList<>();

        // 按日期排序
        Collections.sort(fileList, (f1, f2) -> {
            Date date1 = getLogDate(f1.getName());
            Date date2 = getLogDate(f2.getName());
            return date1.compareTo(date2);

        // 逐个读取log文件并写入新的txt文档
        BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath));
        for (File file : fileList) {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {

    // 获取指定文件夹下的log文件列表
    private static List<File> getLogFiles(String folderPath) {
        List<File> fileList = new ArrayList<>();
        File folder = new File(folderPath);
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".log");
            if (files != null) {
                for (File file : files) {
        return fileList;

    // 从log文件名中提取日期信息
    private static Date getLogDate(String fileName) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        int start = fileName.indexOf("log-") + 4;
        int end = fileName.indexOf(".log");
        String dateString = fileName.substring(start, end);
        return dateFormat.parse(dateString);

About the Author

Question Info

Publish Time
2023-02-27 14:04
Update Time
2023-02-27 14:04