split big log file

Description

一個 log 檔 3G 怎麼看? 不想裝工具, 就只能把檔案切小

Dependencies

JDK7
apache commons io

Codes

    public static void main(String[] params) throws IOException {
        String bigPath = "D:\\logfiles\\biglogfile.log";
        File f = new File(bigPath);
        try (BufferedReader r = new BufferedReader(new FileReader(f))) {
            System.out.println(f.exists());
            int fileCnt = 0;
            List lines = new ArrayList();
            String line;
            while ((line = r.readLine()) != null) {
                if ( lines.size() == 10000 ) {
                    File fileToWrite = new File(f.getParentFile(), f.getName() + "." + fileCnt++);
                    FileUtils.writeLines(fileToWrite, lines);
                    System.out.println("write file:" + fileToWrite);
                    lines.clear();
                }
                lines.add(line);
            }
            File fileToWrite = new File(f.getParentFile(), f.getName() + "." + fileCnt++);
            FileUtils.writeLines(fileToWrite, lines);
            System.out.println("write file:" + fileToWrite);
        } catch (Throwable ex) {
            ex.printStackTrace();
        } 
    }

沒有留言:

張貼留言

Lessons Learned While Using Claude Code Skills

Recently, I started experimenting with Claude Code Skills . I first saw this YouTube video: https://www.youtube.com/watch?v=CEvIs9y1uog ...