{"id":1294,"date":"2022-10-29T22:49:20","date_gmt":"2022-10-29T14:49:20","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1294"},"modified":"2022-11-27T12:39:47","modified_gmt":"2022-11-27T04:39:47","slug":"%e6%b3%9b%e5%9e%8b%e4%b8%8efile-file%e7%b1%bb%e7%9a%84%e5%ae%9e%e4%be%8b%e5%8c%96%e3%80%81file%e7%b1%bb%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%951%e3%80%81file%e7%b1%bb%e7%9a%84%e5%b8%b8%e7%94%a8","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1294","title":{"rendered":"\u6cdb\u578b\u4e0eFile-File\u7c7b\u7684\u5b9e\u4f8b\u5316\u3001File\u7c7b\u7684\u5e38\u7528\u65b9\u6cd51\u3001File\u7c7b\u7684\u5e38\u7528\u65b9\u6cd52"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P577;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.util.Date;\r\n\r\n\/**\r\n * File\u7c7b\u7684\u4f7f\u7528\r\n *\r\n * 1. File\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u6587\u4ef6\u6216\u4e00\u4e2a\u6587\u4ef6\u76ee\u5f55(\u4fd7\u79f0\uff1a\u6587\u4ef6\u5939)\r\n * 2. File\u7c7b\u58f0\u660e\u5728java.io\u5305\u4e0b\r\n * 3. File\u7c7b\u4e2d\u6d89\u53ca\u5230\u5173\u4e8e\u6587\u4ef6\u6216\u6587\u4ef6\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u3001\u4fee\u6539\u65f6\u95f4\u3001\u6587\u4ef6\u5927\u5c0f\u7b49\u65b9\u6cd5\uff0c\r\n *      \u5e76\u672a\u6d89\u53ca\u5230\u5199\u5165\u6216\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u64cd\u4f5c\u3002\u5982\u679c\u9700\u8981\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u5fc5\u987b\u4f7f\u7528IO\u6d41\u6765\u5b8c\u6210\u3002\r\n * 4. \u540e\u7eedFile\u7c7b\u7684\u5bf9\u8c61\u5e38\u4f1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u6d41\u7684\u6784\u9020\u5668\u4e2d\uff0c\u6307\u660e\u8bfb\u53d6\u6216\u5199\u5165\u7684\u7ec8\u70b9.\r\n *\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-29 \u4e0b\u5348 2:53\r\n *\/\r\npublic class FileTest {\r\n    \/*\r\n    1. \u5982\u4f55\u521b\u5efaFile\u7c7b\u7684\u5b9e\u4f8b\r\n        File(String filePath)\r\n        File(String parentPath, String childPath)\r\n        File(File parentPath, String childPath)\r\n    2.\r\n    \u76f8\u5bf9\u8def\u5f84\uff1a\u76f8\u8f83\u4e8e\u67d0\u4e2a\u8def\u5f84\u4e0b\uff0c\u6307\u660e\u7684\u8def\u5f84\u3002\r\n    \u7edd\u5bf9\u8def\u5f84\uff1a\u5305\u542b\u76d8\u7b26\u5728\u5185\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u76ee\u5f55\u7684\u8def\u5f84\r\n\r\n    3.\u8def\u5f84\u5206\u9694\u7b26\r\n        windows:\\\\\r\n        unix:\/\r\n     *\/\r\n\r\n    @Test\r\n    public void test1() {\r\n        \/\/\u6784\u9020\u56681:\r\n        File file1 = new File(\"hello.txt\");\/\/\u76f8\u5bf9\u4e8e\u5f53\u524dmodule\r\n        File file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\JavaSenior\\\\Code\\\\he.txt\");\r\n\r\n        System.out.println(file1);\r\n        System.out.println(file2);\r\n\r\n        \/\/\u6784\u9020\u56682:\r\n        File file3 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\", \"JavaSenior\");\r\n        System.out.println(file3);\r\n\r\n        \/\/\u6784\u9020\u56683:\r\n        File file4 = new File(file3, \"hi.txt\");\r\n        System.out.println(file4);\r\n\r\n    }\r\n\r\n    \/*\r\n        \uf0d8 public String getAbsolutePath()\uff1a\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\r\n        \uf0d8 public String getPath() \uff1a\u83b7\u53d6\u8def\u5f84\r\n        \uf0d8 public String getName() \uff1a\u83b7\u53d6\u540d\u79f0\r\n        \uf0d8 public String getParent()\uff1a\u83b7\u53d6\u4e0a\u5c42\u6587\u4ef6\u76ee\u5f55\u8def\u5f84\u3002\u82e5\u65e0\uff0c\u8fd4\u56denull\r\n        \uf0d8 public long length() \uff1a\u83b7\u53d6\u6587\u4ef6\u957f\u5ea6\uff08\u5373\uff1a\u5b57\u8282\u6570\uff09\u3002\u4e0d\u80fd\u83b7\u53d6\u76ee\u5f55\u7684\u957f\u5ea6\u3002\r\n        \uf0d8 public long lastModified() \uff1a\u83b7\u53d6\u6700\u540e\u4e00\u6b21\u7684\u4fee\u6539\u65f6\u95f4\uff0c\u6beb\u79d2\u503c\r\n\r\n        \u5982\u4e0b\u7684\u4e24\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u76ee\u5f55\uff1a\r\n        \uf0d8 public String&#91;] list() \uff1a\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u76ee\u5f55\u7684\u540d\u79f0\u6570\u7ec4\r\n        \uf0d8 public File&#91;] listFiles() \uff1a\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u76ee\u5f55\u7684File\u6570\u7ec4\r\n\r\n         *\/\r\n    @Test\r\n    public void test2() {\r\n        File file1 = new File(\"hello.txt\");\r\n        File file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\hi.txt\");\r\n\r\n        System.out.println(file1.getAbsolutePath());\r\n        System.out.println(file1.getPath());\r\n        System.out.println(file1.getName());\r\n        System.out.println(file1.getParent());\r\n        System.out.println(file1.length());\r\n        System.out.println(new Date(file1.lastModified()));\r\n\r\n        System.out.println();\r\n\r\n        System.out.println(file2.getAbsolutePath());\r\n        System.out.println(file2.getPath());\r\n        System.out.println(file2.getName());\r\n        System.out.println(file2.getParent());\r\n        System.out.println(file2.length());\r\n        System.out.println(file2.lastModified());\r\n    }\r\n\r\n    @Test\r\n    public void test3() {\r\n        File file = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\JavaSenior\");\r\n\r\n        String&#91;] list = file.list();\r\n        for (String s : list) {\r\n            System.out.println(s);\r\n        }\r\n\r\n        File&#91;] files = file.listFiles();\r\n        for (File f : files) {\r\n            System.out.println(f);\r\n        }\r\n    }\r\n\r\n    \/*\r\n    \uf0d8 public boolean renameTo(File dest):\u628a\u6587\u4ef6\u91cd\u547d\u540d\u4e3a\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\r\n    \u6bd4\u5982\uff1afile1.renameTo(file2)\u4e3a\u4f8b\uff1a\r\n        \u8981\u60f3\u4fdd\u8bc1\u8fd4\u56detrue\uff0c\u9700\u8981file1\u5728\u786c\u76d8\u4e2d\u662f\u5b58\u5728\u7684\uff0c\u4e14file2\u4e0d\u80fd\u5728\u786c\u76d8\u4e2d\u5b58\u5728\u3002\r\n\r\n     *\/\r\n    @Test\r\n    public void test4() {\r\n        File file1 = new File(\"hello.txt\");\r\n        File file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\hi.txt\");\r\n\r\n        boolean renameTo = file2.renameTo(file1);\r\n        System.out.println(renameTo);\r\n    }\r\n\r\n    \/*\r\n    \uf0d8 public boolean isDirectory()\uff1a\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\u76ee\u5f55\r\n    \uf0d8 public boolean isFile() \uff1a\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\r\n    \uf0d8 public boolean exists() \uff1a\u5224\u65ad\u662f\u5426\u5b58\u5728\r\n    \uf0d8 public boolean canRead() \uff1a\u5224\u65ad\u662f\u5426\u53ef\u8bfb\r\n    \uf0d8 public boolean canWrite() \uff1a\u5224\u65ad\u662f\u5426\u53ef\u5199\r\n    \uf0d8 public boolean isHidden() \uff1a\u5224\u65ad\u662f\u5426\u9690\u85cf\r\n\r\n     *\/\r\n    @Test\r\n    public void test5() {\r\n        File file1 = new File(\"hello.txt\");\r\n        file1 = new File(\"hello1.txt\");\r\n\r\n        System.out.println(file1.isDirectory());\r\n        System.out.println(file1.isFile());\r\n        System.out.println(file1.exists());\r\n        System.out.println(file1.canRead());\r\n        System.out.println(file1.canWrite());\r\n        System.out.println(file1.isHidden());\r\n\r\n        System.out.println();\r\n\r\n        File file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\");\r\n        file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io1\");\r\n        System.out.println(file2.isDirectory());\r\n        System.out.println(file2.isFile());\r\n        System.out.println(file2.exists());\r\n        System.out.println(file2.canRead());\r\n        System.out.println(file2.canWrite());\r\n        System.out.println(file2.isHidden());\r\n\r\n    }\r\n\r\n    \/*\r\n    \u521b\u5efa\u786c\u76d8\u4e2d\u5bf9\u5e94\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u76ee\u5f55\r\n    \uf0d8 public boolean createNewFile() \uff1a\u521b\u5efa\u6587\u4ef6\u3002\u82e5\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u4e0d\u521b\u5efa\uff0c\u8fd4\u56defalse\r\n    \uf0d8 public boolean mkdir() \uff1a\u521b\u5efa\u6587\u4ef6\u76ee\u5f55\u3002\u5982\u679c\u6b64\u6587\u4ef6\u76ee\u5f55\u5b58\u5728\uff0c\u5c31\u4e0d\u521b\u5efa\u4e86\u3002\u5982\u679c\u6b64\u6587\u4ef6\u76ee\u5f55\u7684\u4e0a\u5c42\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u4e5f\u4e0d\u521b\u5efa\u3002\r\n    \uf0d8 public boolean mkdirs() \uff1a\u521b\u5efa\u6587\u4ef6\u76ee\u5f55\u3002\u5982\u679c\u4e0a\u5c42\u6587\u4ef6\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u4e00\u5e76\u521b\u5efa\r\n    \u5220\u9664\u78c1\u76d8\u4e2d\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u76ee\u5f55\r\n    \uf0d8 public boolean delete()\uff1a\u5220\u9664\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\r\n        \u5220\u9664\u6ce8\u610f\u4e8b\u9879\uff1a\r\n        Java\u4e2d\u7684\u5220\u9664\u4e0d\u8d70\u56de\u6536\u7ad9\u3002\r\n\r\n     *\/\r\n    @Test\r\n    public void test6() throws IOException {\r\n        \/\/\u6587\u4ef6\u7684\u521b\u5efa\r\n        File file1 = new File(\"hi.txt\");\r\n        if (!file1.exists()) {\r\n            file1.createNewFile();\r\n            System.out.println(\"\u521b\u5efa\u6210\u529f\");\r\n        } else {\/\/\u6587\u4ef6\u5b58\u5728\r\n            file1.delete();\r\n            System.out.println(\"\u5220\u9664\u6210\u529f\");\r\n        }\r\n\r\n    }\r\n\r\n    @Test\r\n    public void test7(){\r\n        \/\/\u6587\u4ef6\u76ee\u5f55\u7684\u521b\u5efa\r\n        File file1 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\io1\\\\io3\");\r\n\r\n        boolean mkdir = file1.mkdir();\r\n        if (mkdir){\r\n            System.out.println(\"\u521b\u5efa\u6210\u529f1\");\r\n        }\r\n\r\n        File file2 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\io1\\\\io4\");\r\n\r\n        boolean mkdir1 = file2.mkdirs();\r\n        if (mkdir1){\r\n            System.out.println(\"\u521b\u5efa\u6210\u529f2\");\r\n        }\r\n\r\n        \/\/\u8981\u60f3\u5220\u9664\u6210\u529f\uff0cio4\u6587\u4ef6\u76ee\u5f55\u4e0b\u4e0d\u80fd\u6709\u5b50\u76ee\u5f55\u6216\u6587\u4ef6\r\n        File file3 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\io1\\\\io4\");\r\n        file3 = new File(\"C:\\\\Users\\\\ZRich\\\\Documents\\\\Code\\\\Java\\\\workspace_idea\\\\io\\\\io1\");\r\n        System.out.println(file3.delete());\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1294","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1294"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1294"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1294\/revisions"}],"predecessor-version":[{"id":1295,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1294\/revisions\/1295"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}