\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}]}}