{"id":1342,"date":"2022-10-30T22:49:21","date_gmt":"2022-10-30T14:49:21","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1342"},"modified":"2023-01-05T18:06:44","modified_gmt":"2023-01-05T10:06:44","slug":"io%e6%b5%81%e4%b8%8e%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b-nio%e4%bb%8b%e7%bb%8d%e5%8f%8anio2%e4%b8%adpath%e3%80%81paths%e3%80%81files%e7%9a%84%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1342","title":{"rendered":"IO\u6d41\u4e0e\u7f51\u7edc\u7f16\u7a0b-NIO\u4ecb\u7ecd\u53caNIO2\u4e2dPath\u3001Paths\u3001Files\u7684\u4ecb\u7ecd"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P618;\n\nimport org.junit.jupiter.api.Test;\n\nimport java.io.File;\nimport java.nio.file.Path;\nimport java.nio.file.Paths;\n\n\/**\n * 1. jdk 7.0 \u65f6\uff0c\u5f15\u5165\u4e86 Path\u3001Paths\u3001Files\u4e09\u4e2a\u7c7b\u3002\n * 2.\u6b64\u4e09\u4e2a\u7c7b\u58f0\u660e\u5728\uff1ajava.nio.file\u5305\u4e0b\u3002\n * 3.Path\u53ef\u4ee5\u770b\u505a\u662fjava.io.File\u7c7b\u7684\u5347\u7ea7\u7248\u672c\u3002\u4e5f\u53ef\u4ee5\u8868\u793a\u6587\u4ef6\u6216\u6587\u4ef6\u76ee\u5f55\uff0c\u4e0e\u5e73\u53f0\u65e0\u5173\n * &lt;p&gt;\n * 4.\u5982\u4f55\u5b9e\u4f8b\u5316Path:\u4f7f\u7528Paths.\n * static Path get(String first, String \u2026 more) : \u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u4e32\u8fde\u6210\u8def\u5f84\n * static Path get(URI uri): \u8fd4\u56de\u6307\u5b9auri\u5bf9\u5e94\u7684Path\u8def\u5f84\n *\n * @author shkstart\n * @create 2019 \u4e0b\u5348 2:44\n *\/\npublic class PathTest {\n\n    \/\/\u5982\u4f55\u4f7f\u7528Paths\u5b9e\u4f8b\u5316Path\n    @Test\n    public void test1() {\n        Path path1 = Paths.get(\"d:\\\\nio\\\\hello.txt\");\/\/new File(String filepath)\n\n        Path path2 = Paths.get(\"d:\\\\\", \"nio\\\\hello.txt\");\/\/new File(String parent,String filename);\n\n        System.out.println(path1);\n        System.out.println(path2);\n\n        Path path3 = Paths.get(\"d:\\\\\", \"nio\");\n        System.out.println(path3);\n    }\n\n    \/\/Path\u4e2d\u7684\u5e38\u7528\u65b9\u6cd5\n    @Test\n    public void test2() {\n        Path path1 = Paths.get(\"d:\\\\\", \"nio\\\\nio1\\\\nio2\\\\hello.txt\");\n        Path path2 = Paths.get(\"hello.txt\");\n\n\/\/\t\tString toString() \uff1a \u8fd4\u56de\u8c03\u7528 Path \u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\n        System.out.println(path1);\n\n\/\/\t\tboolean startsWith(String path) : \u5224\u65ad\u662f\u5426\u4ee5 path \u8def\u5f84\u5f00\u59cb\n        System.out.println(path1.startsWith(\"d:\\\\nio\"));\n\/\/\t\tboolean endsWith(String path) : \u5224\u65ad\u662f\u5426\u4ee5 path \u8def\u5f84\u7ed3\u675f\n        System.out.println(path1.endsWith(\"hello.txt\"));\n\/\/\t\tboolean isAbsolute() : \u5224\u65ad\u662f\u5426\u662f\u7edd\u5bf9\u8def\u5f84\n        System.out.println(path1.isAbsolute() + \"~\");\n        System.out.println(path2.isAbsolute() + \"~\");\n\/\/\t\tPath getParent() \uff1a\u8fd4\u56dePath\u5bf9\u8c61\u5305\u542b\u6574\u4e2a\u8def\u5f84\uff0c\u4e0d\u5305\u542b Path \u5bf9\u8c61\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\n        System.out.println(path1.getParent());\n        System.out.println(path2.getParent());\n\/\/\t\tPath getRoot() \uff1a\u8fd4\u56de\u8c03\u7528 Path \u5bf9\u8c61\u7684\u6839\u8def\u5f84\n        System.out.println(path1.getRoot());\n        System.out.println(path2.getRoot());\n\/\/\t\tPath getFileName() : \u8fd4\u56de\u4e0e\u8c03\u7528 Path \u5bf9\u8c61\u5173\u8054\u7684\u6587\u4ef6\u540d\n        System.out.println(path1.getFileName() + \"~\");\n        System.out.println(path2.getFileName() + \"~\");\n\/\/\t\tint getNameCount() : \u8fd4\u56dePath \u6839\u76ee\u5f55\u540e\u9762\u5143\u7d20\u7684\u6570\u91cf\n\/\/\t\tPath getName(int idx) : \u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e idx \u7684\u8def\u5f84\u540d\u79f0\n        for (int i = 0; i &lt; path1.getNameCount(); i++) {\n            System.out.println(path1.getName(i) + \"*****\");\n        }\n\n\/\/\t\tPath toAbsolutePath() : \u4f5c\u4e3a\u7edd\u5bf9\u8def\u5f84\u8fd4\u56de\u8c03\u7528 Path \u5bf9\u8c61\n        System.out.println(path1.toAbsolutePath());\n        System.out.println(path2.toAbsolutePath());\n\/\/\t\tPath resolve(Path p) :\u5408\u5e76\u4e24\u4e2a\u8def\u5f84\uff0c\u8fd4\u56de\u5408\u5e76\u540e\u7684\u8def\u5f84\u5bf9\u5e94\u7684Path\u5bf9\u8c61\n        Path path3 = Paths.get(\"d:\\\\\", \"nio\");\n        Path path4 = Paths.get(\"nioo\\\\hi.txt\");\n        path3 = path3.resolve(path4);\n        System.out.println(path3);\n\n\/\/\t\tFile toFile(): \u5c06Path\u8f6c\u5316\u4e3aFile\u7c7b\u7684\u5bf9\u8c61\n        File file = path1.toFile();\/\/Path---&gt;File\u7684\u8f6c\u6362\n\n        Path newPath = file.toPath();\/\/File---&gt;Path\u7684\u8f6c\u6362\n\n    }\n\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P618;\n\nimport org.junit.jupiter.api.Test;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.nio.channels.SeekableByteChannel;\nimport java.nio.file.*;\nimport java.util.Iterator;\n\n\/**\n * Files\u5de5\u5177\u7c7b\u7684\u4f7f\u7528\uff1a\u64cd\u4f5c\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5de5\u5177\u7c7b\n * @author shkstart\n * @create 2019 \u4e0b\u5348 2:44\n *\/\npublic class FilesTest {\n\n\t@Test\n\tpublic void test1() throws IOException{\n\t\tPath path1 = Paths.get(\"d:\\\\nio\", \"hello.txt\");\n\t\tPath path2 = Paths.get(\"qaqaq.txt\");\n\t\t\n\/\/\t\tPath copy(Path src, Path dest, CopyOption \u2026 how) : \u6587\u4ef6\u7684\u590d\u5236\n\t\t\/\/\u8981\u60f3\u590d\u5236\u6210\u529f\uff0c\u8981\u6c42path1\u5bf9\u5e94\u7684\u7269\u7406\u4e0a\u7684\u6587\u4ef6\u5b58\u5728\u3002path1\u5bf9\u5e94\u7684\u6587\u4ef6\u6ca1\u6709\u8981\u6c42\u3002\n\/\/\t\tFiles.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);\n\t\t\n\/\/\t\tPath createDirectory(Path path, FileAttribute&lt;?> \u2026 attr) : \u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\n\t\t\/\/\u8981\u60f3\u6267\u884c\u6210\u529f\uff0c\u8981\u6c42path\u5bf9\u5e94\u7684\u7269\u7406\u4e0a\u7684\u6587\u4ef6\u76ee\u5f55\u4e0d\u5b58\u5728\u3002\u4e00\u65e6\u5b58\u5728\uff0c\u629b\u51fa\u5f02\u5e38\u3002\n\t\tPath path3 = Paths.get(\"d:\\\\nio\\\\nio1\");\n\/\/\t\tFiles.createDirectory(path3);\n\t\t\n\/\/\t\tPath createFile(Path path, FileAttribute&lt;?> \u2026 arr) : \u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\n\t\t\/\/\u8981\u60f3\u6267\u884c\u6210\u529f\uff0c\u8981\u6c42path\u5bf9\u5e94\u7684\u7269\u7406\u4e0a\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u3002\u4e00\u65e6\u5b58\u5728\uff0c\u629b\u51fa\u5f02\u5e38\u3002\n\t\tPath path4 = Paths.get(\"d:\\\\nio\\\\hi.txt\");\n\/\/\t\tFiles.createFile(path4);\n\t\t\n\/\/\t\tvoid delete(Path path) : \u5220\u9664\u4e00\u4e2a\u6587\u4ef6\/\u76ee\u5f55\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u6267\u884c\u62a5\u9519\n\/\/\t\tFiles.delete(path4);\n\t\t\n\/\/\t\tvoid deleteIfExists(Path path) : Path\u5bf9\u5e94\u7684\u6587\u4ef6\/\u76ee\u5f55\u5982\u679c\u5b58\u5728\uff0c\u6267\u884c\u5220\u9664.\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u6b63\u5e38\u6267\u884c\u7ed3\u675f\n\t\tFiles.deleteIfExists(path3);\n\t\t\n\/\/\t\tPath move(Path src, Path dest, CopyOption\u2026how) : \u5c06 src \u79fb\u52a8\u5230 dest \u4f4d\u7f6e\n\t\t\/\/\u8981\u60f3\u6267\u884c\u6210\u529f\uff0csrc\u5bf9\u5e94\u7684\u7269\u7406\u4e0a\u7684\u6587\u4ef6\u9700\u8981\u5b58\u5728\uff0cdest\u5bf9\u5e94\u7684\u6587\u4ef6\u6ca1\u6709\u8981\u6c42\u3002\n\/\/\t\tFiles.move(path1, path2, StandardCopyOption.ATOMIC_MOVE);\n\t\t\n\/\/\t\tlong size(Path path) : \u8fd4\u56de path \u6307\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f\n\t\tlong size = Files.size(path2);\n\t\tSystem.out.println(size);\n\n\t}\n\n\t@Test\n\tpublic void test2() throws IOException{\n\t\tPath path1 = Paths.get(\"d:\\\\nio\", \"hello.txt\");\n\t\tPath path2 = Paths.get(\"qaqaq.txt\");\n\/\/\t\tboolean exists(Path path, LinkOption \u2026 opts) : \u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\n\t\tSystem.out.println(Files.exists(path2, LinkOption.NOFOLLOW_LINKS));\n\n\/\/\t\tboolean isDirectory(Path path, LinkOption \u2026 opts) : \u5224\u65ad\u662f\u5426\u662f\u76ee\u5f55\n\t\t\/\/\u4e0d\u8981\u6c42\u6b64path\u5bf9\u5e94\u7684\u7269\u7406\u6587\u4ef6\u5b58\u5728\u3002\n\t\tSystem.out.println(Files.isDirectory(path1, LinkOption.NOFOLLOW_LINKS));\n\n\/\/\t\tboolean isRegularFile(Path path, LinkOption \u2026 opts) : \u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\n\n\/\/\t\tboolean isHidden(Path path) : \u5224\u65ad\u662f\u5426\u662f\u9690\u85cf\u6587\u4ef6\n\t\t\/\/\u8981\u6c42\u6b64path\u5bf9\u5e94\u7684\u7269\u7406\u4e0a\u7684\u6587\u4ef6\u9700\u8981\u5b58\u5728\u3002\u624d\u53ef\u5224\u65ad\u662f\u5426\u9690\u85cf\u3002\u5426\u5219\uff0c\u629b\u5f02\u5e38\u3002\n\/\/\t\tSystem.out.println(Files.isHidden(path1));\n\n\/\/\t\tboolean isReadable(Path path) : \u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u8bfb\n\t\tSystem.out.println(Files.isReadable(path1));\n\/\/\t\tboolean isWritable(Path path) : \u5224\u65ad\u6587\u4ef6\u662f\u5426\u53ef\u5199\n\t\tSystem.out.println(Files.isWritable(path1));\n\/\/\t\tboolean notExists(Path path, LinkOption \u2026 opts) : \u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e0d\u5b58\u5728\n\t\tSystem.out.println(Files.notExists(path1, LinkOption.NOFOLLOW_LINKS));\n\t}\n\n\t\/**\n\t * StandardOpenOption.READ:\u8868\u793a\u5bf9\u5e94\u7684Channel\u662f\u53ef\u8bfb\u7684\u3002\n\t * StandardOpenOption.WRITE\uff1a\u8868\u793a\u5bf9\u5e94\u7684Channel\u662f\u53ef\u5199\u7684\u3002\n\t * StandardOpenOption.CREATE\uff1a\u5982\u679c\u8981\u5199\u51fa\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u3002\u5982\u679c\u5b58\u5728\uff0c\u5ffd\u7565\n\t * StandardOpenOption.CREATE_NEW\uff1a\u5982\u679c\u8981\u5199\u51fa\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u3002\u5982\u679c\u5b58\u5728\uff0c\u629b\u5f02\u5e38\n\t *\n\t * @author shkstart \u90ae\u7bb1\uff1ashkstart@126.com\n\t * @throws IOException\n\t *\/\n\t@Test\n\tpublic void test3() throws IOException{\n\t\tPath path1 = Paths.get(\"d:\\\\nio\", \"hello.txt\");\n\n\/\/\t\tInputStream newInputStream(Path path, OpenOption\u2026how):\u83b7\u53d6 InputStream \u5bf9\u8c61\n\t\tInputStream inputStream = Files.newInputStream(path1, StandardOpenOption.READ);\n\n\/\/\t\tOutputStream newOutputStream(Path path, OpenOption\u2026how) : \u83b7\u53d6 OutputStream \u5bf9\u8c61\n\t\tOutputStream outputStream = Files.newOutputStream(path1, StandardOpenOption.WRITE,StandardOpenOption.CREATE);\n\n\n\/\/\t\tSeekableByteChannel newByteChannel(Path path, OpenOption\u2026how) : \u83b7\u53d6\u4e0e\u6307\u5b9a\u6587\u4ef6\u7684\u8fde\u63a5\uff0chow \u6307\u5b9a\u6253\u5f00\u65b9\u5f0f\u3002\n\t\tSeekableByteChannel channel = Files.newByteChannel(path1, StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);\n\n\/\/\t\tDirectoryStream&lt;Path>  newDirectoryStream(Path path) : \u6253\u5f00 path \u6307\u5b9a\u7684\u76ee\u5f55\n\t\tPath path2 = Paths.get(\"e:\\\\teach\");\n\t\tDirectoryStream&lt;Path> directoryStream = Files.newDirectoryStream(path2);\n\t\tIterator&lt;Path> iterator = directoryStream.iterator();\n\t\twhile(iterator.hasNext()){\n\t\t\tSystem.out.println(iterator.next());\n\t\t}\n\n\n\t}\n}\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-1342","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\/1342"}],"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=1342"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions"}],"predecessor-version":[{"id":3252,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions\/3252"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}