{"id":225152,"date":"2021-09-08T08:50:22","date_gmt":"2021-09-08T00:50:22","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=225152"},"modified":"2021-08-24T09:50:50","modified_gmt":"2021-08-24T01:50:50","slug":"java-source-list","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-source-list.html","title":{"rendered":"Java\u5b9e\u73b0\u5b9a\u65f6\u5907\u4efd\u6587\u4ef6\u6559\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e86Java\u5b9e\u73b0\u5b9a\u65f6\u5907\u4efd\u6587\u4ef6\uff0c\u6587\u4e2d\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86Java\u5982\u4f55\u5b9a\u65f6\u5907\u4efd\u6587\u4ef6\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b<\/p>\n

\u7a0b\u5e8f\u601d\u8def\uff1a<\/p>\n

1.\u7a7a\u76ee\u5f55\u4e0d\u5907\u4efd\uff0c\u4f46\u975e\u7a7a\u76ee\u5f55\u90fd\u5907\u4efd<\/p>\n

2.\u6e90\u76ee\u5f55 source \u8981\u9012\u5f52\u4ed6\u4e0b\u9762\u6240\u6709\u7684\u6587\u4ef6\u548c\u76ee\u5f55 \u5b58\u5165List<\/p>\n

3.\u5faa\u73af\u8fd9\u4e2alist\uff0c\u521b\u5efa\u6bcf\u4e2a\u6587\u4ef6\u7684\u76ee\u5f55<\/p>\n

4.\u5f00\u59cb\u590d\u5236<\/p>\n

\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u5b9a\u65f6\u5907\u4efd\u8def\u5f84\u4e3ae:\\\\a\u7684\u6587\u4ef6\uff0c\u6bcf30\u79d2\u8fdb\u884c\u4e00\u6b21\u5907\u4efd\uff0c\u65f6\u95f4\u53ef\u4fee\u6539\u3002<\/p>\n

\r\npublic class Test12 {\r\n \r\n    public static void main(String[] args) throws InterruptedException {\r\n \r\n        Timer t = new Timer();\r\n        t.scheduleAtFixedRate(new MyTask(),new Date(),1000*30);\r\n \r\n        for(int i = 0;i<10000;i++){\r\n            Thread.sleep(1000);\r\n            System.out.println(\"warning\");\r\n        }\r\n \r\n \r\n    }\r\n \r\n}\r\n \r\nclass MyTask extends TimerTask{\r\n \r\n    static final String SOURCE = \"e:\\\\a\";\r\n    static String DEST;\r\n \r\n    @Override\r\n    public void run() {\r\n \r\n        Date d = new Date();\r\n        DateFormat df = new SimpleDateFormat(\"yyyy-MM-dd_HH-mm-ss\");\r\n        DEST = \"e:\\\\dest_\" + df.format(d);\r\n        File file = new File(SOURCE);\r\n        File dest = new File(DEST);\r\n \r\n        if(dest.exists()){\r\n            deleteAll(dest);\r\n        }\r\n        System.out.println(\"\u521b\u5efa\u5907\u4efd\u76ee\u5f55\uff1f\"+dest.mkdirs());\r\n        List list = new ArrayList<\/file>();\r\n \r\n        getAllFile(file,list);\r\n \r\n        backUp(list,dest);\r\n    }\r\n \r\n    \/\/\u5907\u4efd\r\n    private static void backUp(List<\/file> list, File dest) {\r\n \r\n        if (list == null || list.size() <= 0) {\r\n            return;\r\n        }\r\n        for (File f : list) {\r\n            String fpath = f.getAbsolutePath();     \/\/\u53d6\u51fa\u7edd\u5bf9\u8def\u5f84        e:\\\\a\r\n            String newpath = fpath.replace(SOURCE, DEST);\r\n            System.out.println(\"\u5bf9\u5e94\u7684\u65b0\u8def\u5f84\" + newpath);\r\n            File newFile = new File(newpath);\r\n            if (newFile.getParentFile().exists() == false) {\r\n                System.out.println(\"\u521b\u5efa\" + newFile + \"\u7684\u7236\u76ee\u5f55\u6210\u529f\uff1f\" + newFile.getParentFile().mkdirs());\r\n            }\r\n            if (f.isFile()) {\r\n                copy(f, newFile);\r\n            }\r\n        }\r\n \r\n    }\r\n \r\n    \/\/\u590d\u5236\r\n    private static void copy(File inFile, File outFile) {\r\n        FileInputStream fis = null;\r\n        FileOutputStream fos = null;\r\n        boolean isFlag = false;\r\n \r\n        try {\r\n            fis = new FileInputStream(inFile);\r\n            fos = new FileOutputStream(outFile);\r\n \r\n            byte[] bs = new byte[1024];\r\n            int length = -1;\r\n            while ((length = fis.read(bs)) != -1) {\r\n                fos.write(bs, 0, length);\r\n            }\r\n            fos.flush();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (fos != null) {\r\n                try {\r\n                    fos.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n            if (fis != null) {\r\n                try {\r\n                    fis.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }\r\n    }\r\n \r\n    \/\/\u9012\u5f52\u83b7\u53d6\u539f\u76ee\u5f55\u4e0b\u6240\u6709\u7684\u6587\u4ef6\u4fe1\u606f\r\n    private static void getAllFile(File source, List<\/file> list) {\r\n        if (source.isDirectory()) {\r\n            \/\/\u67e5\u770b\u5b50\u76ee\u5f55 listFile()\r\n            File[] fs = source.listFiles();\r\n            if (fs != null && fs.length > 0) {\r\n                \/\/\u8bf4\u660e\u6709\u5b50\u76ee\u5f55\u6216\u5b50\u6587\u4ef6\r\n                for (File ff : fs) {\r\n                    getAllFile(ff, list);\r\n                }\r\n            }\r\n \r\n        }\r\n        list.add(source);\r\n \r\n    }\r\n \r\n    \/\/\u9012\u5f52\u5220\u9664\r\n    private static void deleteAll(File f) {\r\n        if (f.isDirectory()) {\r\n            File[] fs = f.listFiles();\r\n            if (fs != null && fs.length > 0) {\r\n                for (File file : fs) {\r\n                    deleteAll(file);\r\n                }\r\n            }\r\n        }\r\n        System.out.println(f + \"\u5220\u9664\u6210\u529f\uff1f\" + f.delete());\r\n    }\r\n \r\n}\r\n<\/file><\/pre>\n

\u518d\u4e3a\u5927\u5bb6\u8865\u5145\u4e00\u6bb5\uff1a<\/p>\n

\r\nimport java.util.Calendar;\r\nimport java.util.Date;\r\nimport java.text.SimpleDateFormat;\r\nimport java.io.IOException;\r\nimport java.io.PrintStream;\r\n  \r\npublic class Backup \r\n{\r\n public static void main(String[] args) \r\n {\r\n  Runtime runtime = Runtime.getRuntime();\r\n  Calendar calendar = Calendar.getInstance();\r\n  SimpleDateFormat dateFormat = new SimpleDateFormat(\"yyyyMMdd_HHmmss\");\r\n  String currentTime = dateFormat.format(calendar.getTime());\r\n  Process p = null;\r\n  PrintStream print = null;\r\n  StringBuilder buf = new StringBuilder();\r\n  for(String a : args){\r\n   buf.append(a);\r\n   buf.append(\" \");\r\n  }\r\n  String databases = buf.toString();\r\n   \r\n  try{\r\n   p = runtime.exec(\"cmd \/c mysqldump -uroot -p1234 -B \"+databases+\">\"+currentTime+\".sql.bak\");\r\n  }catch (IOException e){\r\n   if( p != null ){\r\n    p.destroy();\r\n   }\r\n   try{\r\n    print = new PrintStream(currentTime+\"_backup_err.log\");\r\n    dateFormat.applyPattern(\"yyyy-MM-dd HH:mm:ss\");\r\n    currentTime = dateFormat.format(calendar.getTime());\r\n    print.println(currentTime+\"  backup failed.\");\r\n    e.printStackTrace(print);\r\n    print.flush();\r\n   }catch (IOException e2){\r\n  \r\n   }finally{\r\n    if(print!=null){\r\n     print.close();\r\n    }\r\n   }\r\n  }\r\n }\r\n}\r\n<\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86Java\u5982\u4f55\u5b9a\u65f6\u5907\u4efd\u6587\u4ef6\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b \u7a0b\u5e8f\u601d\u8def\uff1a 1.\u7a7a\u76ee\u5f55\u4e0d […]<\/p>\n","protected":false},"author":362,"featured_media":95740,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-225152","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\/225152","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\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=225152"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225152\/revisions"}],"predecessor-version":[{"id":225154,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225152\/revisions\/225154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/95740"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=225152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=225152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=225152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}