0
0

javaでログをマージする

Posted at

コード

public class Main {

	
	public static final String BACKUPDIR = "C:\\hoge\\backup";
	public static final String MERGEDDIR = "C:\\hoge\\merged";
	public static List<String> bkDirs;
	public static List<String> bkFileNames;
	
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		
		
	}
	
	
	public void filenameListOut() {
		// backupフォルダのパスを取得
				Path backupDir = Paths.get(BACKUPDIR);		
				
				// 
				bkFileNames = new ArrayList<String>();
				// backupフォルダ配下のフォルダ名を取得
				try {
					Files.walk(backupDir)
					.filter(Files::isRegularFile) // ファイルのみ対象
					.forEach(path -> {
						String fileName = path.getFileName().toString();
						bkFileNames.add(fileName);
					});
				} catch (IOException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
				
				for(var str : bkFileNames){
					System.out.println(str);
					
					Path p = Paths.get(MERGEDDIR + "\\" + str);
					if(Files.exists(p)) {
						// ファイルが存在する場合
					}else {
						// ファイルが存在しない場合作成する
						
					}
				}
	}

}

ファイル作成

import java.io.IOException;
import java.nio.file.*;

public class FileCheckAndCreate {
    public static void main(String[] args) {
        // 作成したいファイルのパスを指定
        Path filePath = Paths.get("C:\\hoge\\output.txt");

        // ファイルの存在チェック
        if (Files.exists(filePath)) {
            System.out.println("ファイルは既に存在します: " + filePath);
        } else {
            try {
                // ファイルが存在しない場合は作成
                Files.createFile(filePath);
                System.out.println("新しいファイルを作成しました: " + filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class FileMerger {
    public static void main(String[] args) throws IOException {
        // backupフォルダとmergedフォルダのパスを指定
        Path backupDir = Paths.get("C:\\hoge\backup");
        Path mergedDir = Paths.get("C:\\hoge\\merged");

        // mergedフォルダが存在しない場合は作成
        if (!Files.exists(mergedDir)) {
            Files.createDirectory(mergedDir);
        }

        // ファイルマッピング用のMapを作成 (ファイル名 -> List<ファイルパス>)
        Map<String, List<Path>> fileMap = new HashMap<>();

        // backupフォルダ配下のすべてのサブフォルダおよびファイルを探索
        Files.walk(backupDir)
            .filter(Files::isRegularFile) // ファイルのみ対象
            .filter(path -> path.toString().endsWith(".txt")) // テキストファイルのみ
            .forEach(path -> {
                String fileName = path.getFileName().toString();
                fileMap.computeIfAbsent(fileName, k -> new ArrayList<>()).add(path);
            });

        // ファイルを結合してmergedフォルダに出力
        for (Map.Entry<String, List<Path>> entry : fileMap.entrySet()) {
            String fileName = entry.getKey();
            List<Path> filePaths = entry.getValue();
            Path mergedFile = mergedDir.resolve(fileName);

            // ファイルの結合処理
            try (BufferedWriter writer = Files.newBufferedWriter(mergedFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
                for (Path filePath : filePaths) {
                    byte[] fileContent = Files.readAllBytes(filePath); // ファイルをバイト単位で読み込む
                    writer.write(new String(fileContent, StandardCharsets.UTF_8)); // 文字列として書き込む
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("ファイルの結合が完了しました!");
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0