{"id":1264,"date":"2019-08-28T15:56:06","date_gmt":"2019-08-28T07:56:06","guid":{"rendered":"https:\/\/199604.com\/?p=1264"},"modified":"2019-08-29T23:07:20","modified_gmt":"2019-08-29T15:07:20","slug":"%e8%ae%b0%e5%bf%86%e7%b3%bb%e5%88%97-java-io%e7%9a%84file%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/199604.com\/1264","title":{"rendered":"\u8bb0\u5fc6\u7cfb\u5217-Java IO\u7684File\u7c7b"},"content":{"rendered":"<p>\u53c8\u662f\u5931\u4e1a\u5b66\u4e60\u7684\u4e00\u5929~\u5657\u5472\u6700\u8fd1\u9762\u8bd5\u5bf9io\u6d41\u6709\u70b9\u4e0d\u8bb0\u5f97\uff0c\u4e8e\u662f\u7ffb\u5f00\u4e86\u4e45\u8fddJAVA\u56db\u5927\u540d\u8457\u4e4bjava\u4ece\u5165\u95e8\u5230\u653e\u5f03\u91cd\u65b0\u590d\u4e60\uff0c\u987a\u4fbf\u5f04\u4e00\u4e2aIO\u7cfb\u5217\u4ee5\u540e\u81ea\u5df1\u76f4\u63a5\u770b\u7b14\u8bb0\u7b97\u4e86~<\/p>\n<hr \/>\n<h2>File\u7c7b<\/h2>\n<p>File\u7c7b\u662fjava.io\u5305\u4e0b\u4ee3\u8868\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u5e0c\u671b\u5728\u7a0b\u5e8f\u4e2d\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7File\u7c7b\u6765\u5b8c\u6210\u3002\u503c\u5f97\u6307\u51fa\u7684\u662f\uff0c\u4e0d\u7ba1\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u90fd\u662f\u4f7f\u7528File\u6765\u64cd\u4f5c\u7684\uff0cFile\u80fd\u65b0\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u6587\u4ef6\u548c\u76ee\u5f55\uff0cFile\u4e0d\u80fd\u8bbf\u95ee\u6587\u4ef6\u5185\u5bb9\u672c\u8eab\u3002\u5982\u679c\u9700\u8981\u8bbf\u95ee\u6587\u4ef6\u5185\u5bb9\u672c\u8eab\uff0c\u5219\u9700\u8981\u4f7f\u7528\u8f93\u5165\/\u8f93\u51fa\u6d41\u3002<\/p>\n<p>File\u7c7b\u5171\u63d0\u4f9b\u4e86\u4e09\u4e2a\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570:<\/p>\n<pre class=\"lang:default decode:true\">1.File (String pathname) &amp;nbsp;&amp;nbsp;\r\n\u4f8b:File f1 = new File(\"test1.txt\"); \/\/\u521b\u5efa\u6587\u4ef6\u5bf9\u8c61f1,f1\u6240\u6307\u7684\u6587\u4ef6\u662f\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u7684test1.txt\r\n2.File (String parent,String child)\r\n\u4f8b:File f2 = new &amp;nbsp;File(\"D:\\\\testDir2\",\"test2.txt\") ;\/\/\u6ce8\u610f\uff1aD:\\\\testDir2\u76ee\u5f55\u4e8b\u5148\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u5f02\u5e38\r\n3.File(File parent, String child)\r\n\u4f8b:\r\nString fileName = \"D:\"+File.separator+\"testDir3\"+File.separator;\r\nFile f3 = new File(fileName);\r\nif(!f3.exists()&amp;&amp;f3.isDirectory()){\r\n\tf3.mkdir();\r\n}\r\nFile f4 = new File(f3, \"test3.txt\");\r\nif(!f4.exists()&amp;&amp;!f4.isFile()){\/\/ \u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728,\u521b\u5efa\u6587\u4ef6\r\n\ttry {\r\n\t\tf4.createNewFile();\r\n\t} catch (IOException e) {\r\n\t\t\/\/ TODO Auto-generated catch block\r\n\t}\r\n}\r\nSystem.out.println(f4.toString());\/\/D:\\testDir3\\test3.txt<\/pre>\n<h3>\u6587\u4ef6\u5206\u9694\u7b26<\/h3>\n<p>\u6ce8\u610f\uff1a\u6211\u4eec\u77e5\u9053\uff0c\u5404\u4e2a\u5e73\u53f0\u4e4b\u95f4\u7684\u8def\u5f84\u5206\u9694\u7b26\u662f\u4e0d\u4e00\u6837\u7684\u3002\u6240\u4ee5\u4e3a\u4e86\u5c4f\u853d\u5404\u4e2a\u5e73\u53f0\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u5dee\u5f02\u5c31\u4f7f\u7528JDK \u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u5b57\u6bb5\u3002<\/p>\n<p>ps\uff1aWindows\u5728\u8def\u5f84\u540d\u4e2d\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u4f5c\u4e3a\u540d\u79f0\u5206\u9694\u7b26\uff0c\u800cUNIX\u4f7f\u7528\u6b63\u659c\u6760\uff08\/\uff09\u3002<\/p>\n<pre class=\"lang:default decode:true \">--windows\u8f93\u51fa\r\nSystem.out.println(File.separator);\/\/\u8f93\u51fa \\  --\u662f\u7528\u6765\u5206\u9694\u540c\u4e00\u4e2a\u8def\u5f84\u5b57\u7b26\u4e32\u4e2d\u7684\u76ee\u5f55\u7684\r\nSystem.out.println(File.pathSeparator);\/\/\u8f93\u51fa ; --\u662f\u5206\u9694\u8fde\u7eed\u591a\u4e2a\u8def\u5f84\u5b57\u7b26\u4e32\u7684\u5206\u9694\u7b26<\/pre>\n<p>\u521b\u5efaFile\u5bf9\u8c61\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u5217\u8868\u4e2d\u7684\u65b9\u6cd5\u64cd\u4f5c\u6587\u4ef6\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/199604.com\/wp-content\/uploads\/2019\/08\/io-file20190828153851.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1265 size-full\" data-original=\"https:\/\/199604.com\/wp-content\/uploads\/2019\/08\/io-file20190828153851.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" width=\"506\" height=\"2206\"  sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/div><\/p>\n<p>(\u8868\u683c\u4ece\u83dc\u9e1f\u6559\u7a0b\u590d\u5236\uff1ahttps:\/\/www.runoob.com\/java\/java-file.html)<\/p>\n<p>\u770b\u5230\u61f5\u903c\u4e86?\u65b9\u6cd5\u662f\u4e0d\u662f\u6709\u70b9\u591a\uff1f\u5176\u5b9e\u6211\u81ea\u5df1\u5de5\u4f5c\u4e00\u5e74\u591a\u6765\u5e38\u7528\u5c31\u8fd9\u51e0\u4e2a\uff1a<\/p>\n<h3>1.\u521b\u5efa\u65b9\u6cd5\uff1a<\/h3>\n<p>1.boolean createNewFile() \u4e0d\u5b58\u5728\u8fd4\u56detrue \u5b58\u5728\u8fd4\u56defalse<br \/>\n2.boolean mkdir() \u521b\u5efa\u76ee\u5f55\uff0c\u5982\u679c\u4e0a\u4e00\u7ea7\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u5931\u8d25<br \/>\n3.boolean mkdirs() \u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff0c\u5982\u679c\u4e0a\u4e00\u7ea7\u76ee\u5f55\u4e0d\u5b58\u5728\u4e5f\u4f1a\u81ea\u52a8\u521b\u5efa<\/p>\n<h3>2.\u5220\u9664\u65b9\u6cd5\uff1a<\/h3>\n<p>1.boolean delete() \u5220\u9664\u6587\u4ef6\u6216\u76ee\u5f55\uff0c\u5982\u679c\u662f\u5220\u9664\u76ee\u5f55\uff0c\u5219\u76ee\u5f55\u4e0b\u5fc5\u987b\u4e3a\u7a7a\u624d\u80fd\u5220\u9664\uff0c\u4e0d\u7136\u62a5\u5f02\u5e38<br \/>\n2.boolean deleteOnExit() \u6587\u4ef6\u4f7f\u7528\u5b8c\u6210\u540e\u5220\u9664<\/p>\n<h3>3.\u5224\u5b9a\u65b9\u6cd5:<\/h3>\n<p>1.boolean canExecute()\u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u6267\u884c<br \/>\n2.boolean canRead()\u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u8bfb<br \/>\n3.boolean canWrite() \u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u5199<br \/>\n4.boolean exists() \u5224\u65ad\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<br \/>\n5.boolean isDirectory() \u5224\u65ad\u6b64\u8def\u5f84\u662f\u5426\u4e3a\u4e00\u4e2a\u76ee\u5f55<br \/>\n6.boolean isFile()\u3000\u3000\u5224\u65ad\u662f\u5426\u4e3a\u4e00\u4e2a\u6587\u4ef6<br \/>\n7.boolean isHidden()\u3000\u3000\u5224\u65ad\u662f\u5426\u4e3a\u9690\u85cf\u6587\u4ef6<br \/>\n8.boolean isAbsolute()\u5224\u65ad\u662f\u5426\u662f\u7edd\u5bf9\u8def\u5f84 \u6587\u4ef6\u4e0d\u5b58\u5728\u4e5f\u80fd\u5224\u65ad<\/p>\n<h3>4.\u83b7\u53d6\u65b9\u6cd5\uff1a<\/h3>\n<p>1.String getName() \u83b7\u53d6\u6b64\u8def\u5f84\u8868\u793a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u540d\u79f0<br \/>\n2.String getPath() \u5c06\u6b64\u8def\u5f84\u540d\u8f6c\u6362\u4e3a\u8def\u5f84\u540d\u5b57\u7b26\u4e32<br \/>\n3.String getAbsolutePath() \u8fd4\u56de\u6b64\u62bd\u8c61\u8def\u5f84\u540d\u7684\u7edd\u5bf9\u5f62\u5f0f<br \/>\n4.String getParent()\/\/\u5982\u679c\u6ca1\u6709\u7236\u76ee\u5f55\u8fd4\u56denull<br \/>\n5.long lastModified()\/\/\u83b7\u53d6\u6700\u540e\u4e00\u6b21\u4fee\u6539\u7684\u65f6\u95f4&#8211;&gt;\u6beb\u79d2\u503c\uff0c\u5982\u679c\u9700\u8981\u770b\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u7684\u8bdd\uff0c\u9700\u8981\u521b\u5efa\u65e5\u5386\u7c7b?\u7136\u540e\u901a\u8fc7\u65e5\u5386\u7c7b\u7684set\u65b9\u6cd5\u628a\u503c\u4f20\u8fdb\u53bb\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u5177\u4f53yyyymmdd\u4e4b\u7c7b\u7684\u5566<br \/>\n6.long length() \u8fd4\u56de\u7531\u6b64\u62bd\u8c61\u8def\u5f84\u540d\u8868\u793a\u7684\u6587\u4ef6\u7684\u957f\u5ea6\u3002<br \/>\n7.boolean renameTo(File f) \u91cd\u547d\u540d\u7531\u6b64\u62bd\u8c61\u8def\u5f84\u540d\u8868\u793a\u7684\u6587\u4ef6\u3002<br \/>\n8.File[] liseRoots()\/\/\u83b7\u53d6\u673a\u5668\u76d8\u7b26<br \/>\n9.String[] list() \u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u547d\u540d\u7531\u6b64\u62bd\u8c61\u8def\u5f84\u540d\u8868\u793a\u7684\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002<br \/>\n10.String[] list(FilenameFilter filter) \u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u547d\u540d\u7531\u6b64\u62bd\u8c61\u8def\u5f84\u540d\u8868\u793a\u7684\u76ee\u5f55\u4e2d\u6ee1\u8db3\u6307\u5b9a\u8fc7\u6ee4\u5668\u7684\u6587\u4ef6\u548c\u76ee\u5f55<\/p>\n<p>\u6700\u6700\u6700\u540e\u5c31\u662f\u4e00\u4e2a\u5c0f\u4f8b\u5b50\u5427~\u55b7\u4e0b\u4ee5\u540e\u56de\u6765\u770b\u7684\u5783\u573e\u90ed\u826f\u4fca,\u597d\u597d\u770b\u54c8~<\/p>\n<pre class=\"lang:default decode:true \">package com.htjf.util;\r\n\r\nimport java.io.File;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\n\/**\r\n * File\u6587\u4ef6\u5de5\u5177\u7c7b\r\n * @author Jiomer\r\n *\r\n *\/\r\npublic class FileUtil {\r\n\t\r\n\t\/**\r\n\t * \u83b7\u53d6\u672c\u5730\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u7684\u6587\u4ef6\r\n\t * isAddDirectory \u662f\u5426\u52a0\u8f7d\u5b50\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\r\n\t * @param filepath \u6587\u4ef6\u5939\u8def\u5f84\r\n\t * @return\r\n\t *\/\r\n\tpublic static List&lt;String&gt; getListNames(String filepath,boolean isAddDirectory){\r\n\t\tList&lt;String&gt; listName = new ArrayList&lt;String&gt;();  \r\n\t\tFile baseFile= new File(filepath);  \r\n\t\tif (baseFile.isFile() || !baseFile.exists()) {\r\n            return listName;\r\n        }\r\n\t\tFile[] files = baseFile.listFiles();\r\n\t\tfor (File file : files) {\r\n            if (file.isDirectory()) {\r\n                if(isAddDirectory){\r\n                \tlistName.add(file.getName());\r\n                }\r\n                listName.addAll(getListNames(file.getAbsolutePath(),isAddDirectory));\r\n            } else {\r\n            \tlistName.add(file.getName());\r\n            }\r\n        }\r\n\t\treturn listName;\r\n\t}\r\n\t\r\n\t\/**\r\n\t * \u6587\u4ef6\u5904\u7406\u524d\u91cd\u547d\u540d(\u4ee5\u6269\u5c55\u540d\u66ff\u6362\u7684\u5f62\u5f0f)\r\n\t * @param srcFile \u9700\u8981\u91cd\u547d\u540d\u7684\u6587\u4ef6\r\n\t * @param tar \u91cd\u547d\u540d\u76ee\u6807\u5b57\u7b26\u4e32\r\n\t * @param rep \u66ff\u6362\u5b57\u7b26\u4e32\r\n\t * @return \u91cd\u547d\u540d\u540e\u6587\u4ef6\r\n\t *\/\r\n\tpublic static File rename(File srcFile, String tar, String rep) {\r\n\t\tString perMatchFileName =  srcFile.getPath().replace(tar, rep);\r\n\t\tFile newNameFile = new File(perMatchFileName);\r\n\t\tsrcFile.renameTo(newNameFile);\r\n\t\treturn newNameFile;\r\n\t}\r\n\tpublic static String reFileName(String oldName, String tar, String rep) {\r\n\t\tString newNameFile =  oldName.replace(tar, rep);\r\n\t\treturn newNameFile;\r\n\t}\r\n\t\/**\r\n\t * \u5220\u9664\u6587\u4ef6\r\n\t * @param file\r\n\t * @return\r\n\t *\/\r\n\tpublic static boolean delFile(File file){\r\n\/\/\t\tFile file=new File(filePath+File.separator+fileName);\r\n\t\tif(file.exists()&amp;&amp;file.isFile()){\r\n\t\t\treturn file.delete();\r\n\t\t}else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/pre>\n<hr \/>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/zhifubao.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/zhifubao.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"318\" height=\"406\" \/><\/div><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/weixin.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/weixin.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"268\" height=\"291\" \/><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c8\u662f\u5931\u4e1a\u5b66\u4e60\u7684\u4e00\u5929~\u5657\u5472\u6700\u8fd1\u9762\u8bd5\u5bf9io\u6d41\u6709\u70b9\u4e0d\u8bb0\u5f97\uff0c\u4e8e\u662f\u7ffb\u5f00\u4e86\u4e45\u8fddJAVA\u56db\u5927\u540d\u8457\u4e4bjava\u4ece\u5165\u95e8\u5230\u653e\u5f03\u91cd\u65b0\u590d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[218,217,129],"class_list":["post-1264","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-file","tag-io","tag-java"],"_links":{"self":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/comments?post=1264"}],"version-history":[{"count":4,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":1275,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1264\/revisions\/1275"}],"wp:attachment":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/media?parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/categories?post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/tags?post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}