{"id":173011,"date":"2020-03-01T10:47:34","date_gmt":"2020-03-01T02:47:34","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173011"},"modified":"2020-02-24T09:52:15","modified_gmt":"2020-02-24T01:52:15","slug":"java-read-log","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-read-log.html","title":{"rendered":"Java\u5b9e\u65f6\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u5b9e\u4e60\u7684\u516c\u53f8\u78b0\u5230\u4e00\u4e2a\u53e4\u602a\u7684\u9700\u6c42\uff1a\u5728\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u5f53\u65e5\u5fd7\u6587\u4ef6\u5199\u5230\u4e00\u5b9a\u5927\u5c0f\u65f6\uff0c\u6bd4\u5982\u662f1G\uff0c\u4f1a\u5c06\u8fd9\u4e2a\u65e5\u5fd7\u6587\u4ef6\u6539\u540d\u6210\u53e6\u4e00\u4e2a\u540d\u5b57\uff0c\u5e76\u65b0\u5efa\u4e00\u4e2a\u4e0e\u539f\u6587\u4ef6\u540d\u76f8\u540c\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u518d\u5f80\u8fd9\u4e2a\u65b0\u5efa\u7684\u65e5\u5fd7\u6587\u4ef6\u91cc\u5199\u6570\u636e\uff1b\u8981\u6c42\u5199\u4e00\u4e2a\u7a0b\u5e8f\u80fd\u5b9e\u65f6\u5730\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u4e0d\u80fd\u5f71\u54cd\u5199\u64cd\u4f5c\u4e0e\u91cd\u547d\u540d\u64cd\u4f5c\u3002
\nRandomAccessFile\u7c7b\u4e2dseek\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6307\u5b9a\u4f4d\u7f6e\u8bfb\u53d6\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6587\u4ef6\u5b9e\u65f6\u8bfb\u53d6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/a><\/p>\n

\u7f16\u7801\u5b9e\u73b0<\/strong><\/div>\n

\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u79d2\u5199200\u6761\u8bb0\u5f55\uff0c\u5e76\u4e14\u8bb0\u5f55\u5199\u7684\u65f6\u95f4<\/p>\n

\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.io.RandomAccessFile;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n\r\npublic class LogReader implements Runnable {\r\n    private File logFile = null;\r\n    private long lastTimeFileSize = 0; \/\/ \u4e0a\u6b21\u6587\u4ef6\u5927\u5c0f\r\n    private static SimpleDateFormat dateFormat = new SimpleDateFormat(\r\n            \"yyyy-MM-dd HH:mm:ss\");\r\n\r\n    public LogReader(File logFile) {\r\n        this.logFile = logFile;\r\n        lastTimeFileSize = logFile.length();\r\n    }\r\n\r\n    \/**\r\n     * \u5b9e\u65f6\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\r\n     *\/\r\n    public void run() {\r\n        while (true) {\r\n            try {\r\n                long len = logFile.length();\r\n                if (len < lastTimeFileSize) {\r\n                    System.out.println(\"Log file was reset. Restarting logging from start of file.\");\r\n                    lastTimeFileSize = len;\r\n                } else if(len > lastTimeFileSize) {\r\n                    RandomAccessFile randomFile = new RandomAccessFile(logFile, \"r\");\r\n                    randomFile.seek(lastTimeFileSize);\r\n                    String tmp = null;\r\n                    while ((tmp = randomFile.readLine()) != null) {\r\n                        System.out.println(dateFormat.format(new Date()) + \"\\t\"\r\n                                + tmp);\r\n                    }\r\n                    lastTimeFileSize = randomFile.length();\r\n                    randomFile.close();\r\n                }\r\n            } catch (IOException e) {\r\n                \/\/ TODO Auto-generated catch block\r\n                e.printStackTrace();\r\n            }\r\n            \r\n            try {\r\n                Thread.sleep(50);\r\n            } catch (InterruptedException e) {\r\n                \/\/ TODO Auto-generated catch block\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n

\u5b9e\u65f6\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u96941\u79d2\u8bfb\u4e00\u6b21<\/p>\n

\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.io.RandomAccessFile;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n\r\npublic class LogReader implements Runnable {\r\n    private File logFile = null;\r\n    private long lastTimeFileSize = 0; \/\/ \u4e0a\u6b21\u6587\u4ef6\u5927\u5c0f\r\n    private static SimpleDateFormat dateFormat = new SimpleDateFormat(\r\n            \"yyyy-MM-dd HH:mm:ss\");\r\n\r\n    public LogReader(File logFile) {\r\n        this.logFile = logFile;\r\n        lastTimeFileSize = logFile.length();\r\n    }\r\n\r\n    \/**\r\n     * \u5b9e\u65f6\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\r\n     *\/\r\n    public void run() {\r\n        while (true) {\r\n            try {\r\n                RandomAccessFile randomFile = new RandomAccessFile(logFile, \"r\");\r\n                randomFile.seek(lastTimeFileSize);\r\n                String tmp = null;\r\n                while ((tmp = randomFile.readLine()) != null) {\r\n                    System.out.println(dateFormat.format(new Date()) + \"\\t\"\r\n                            + tmp);\r\n                }\r\n                lastTimeFileSize = randomFile.length();\r\n            } catch (IOException e) {\r\n                \/\/ TODO Auto-generated catch block\r\n                e.printStackTrace();\r\n            }\r\n            try {\r\n                Thread.sleep(500);\r\n            } catch (InterruptedException e) {\r\n                \/\/ TODO Auto-generated catch block\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n

\u5f00\u542f\u5199\u7ebf\u7a0b\u3001\u8bfb\u7ebf\u7a0b\uff0c\u5c06\u5b9e\u65f6\u4fe1\u606f\u6253\u5370\u5728\u63a7\u5236\u53f0\u3002<\/p>\n

\r\nimport java.io.File;\r\n\r\npublic class RunRun {\r\n    public static void main(String[] args) {\r\n        File logFile = new File(\"mock.log\");\r\n        Thread wthread = new Thread(new LogWrite(logFile));\r\n        wthread.start();\r\n        Thread rthread = new Thread(new LogReader(logFile));\r\n        rthread.start();\r\n    }\r\n}\r\n<\/pre>\n

\u5728\u8bfb\u5199\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u5c06mock.log\u6587\u4ef6\u91cd\u547d\u540d\uff0c\u53d1\u73b0\u4f9d\u65e7\u53ef\u4ee5\u5b9e\u65f6\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5199\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u79d2\u5199200\u6761\u8bb0\u5f55\uff0c\u5e76\u4e14\u8bb0\u5f55\u5199\u7684\u65f6\u95f4 import java.io.File; import ja […]<\/p>\n","protected":false},"author":1481,"featured_media":172972,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-173011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173011"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173011\/revisions"}],"predecessor-version":[{"id":173013,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173011\/revisions\/173013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/172972"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}