6.NIO2-Path、Paths、Files

NIO.2

jdk1.7中,java对 NIO 极大的扩展,主要增强的是对文件处理 和 文件系统特性的支持

 

关于其中一些API的使用

  1 public class TestNIO_2_Path_File {
  2     
  3     //自动资源管理:自动关闭实现 AutoCloseable 接口的资源
  4     /*@Test
  5         public void test8(){
  6             try(FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
  7                     FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)){
  8                 
  9                 ByteBuffer buf = ByteBuffer.allocate(1024);
 10                 inChannel.read(buf);
 11                 
 12             }catch(IOException e){
 13                 
 14             }
 15         }*/
 16     
 17     /*
 18     Files常用方法:用于操作内容
 19         SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 获取与指定文件的连接,how 指定打开方式。
 20         DirectoryStream newDirectoryStream(Path path) : 打开 path 指定的目录
 21         InputStream newInputStream(Path path, OpenOption…how):获取 InputStream 对象
 22         OutputStream newOutputStream(Path path, OpenOption…how) : 获取 OutputStream 对象
 23      */
 24     @Test
 25     public void test7() throws IOException{
 26         SeekableByteChannel newByteChannel = Files.newByteChannel(Paths.get("1.jpg"), StandardOpenOption.READ);
 27         
 28         DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get("e:/"));
 29         
 30         for (Path path : newDirectoryStream) {
 31             System.out.println(path);
 32         }
 33     }
 34     
 35     /*
 36     Files常用方法:用于判断
 37         boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
 38         boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
 39         boolean isExecutable(Path path) : 判断是否是可执行文件
 40         boolean isHidden(Path path) : 判断是否是隐藏文件
 41         boolean isReadable(Path path) : 判断文件是否可读
 42         boolean isWritable(Path path) : 判断文件是否可写
 43         boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在
 44         public static <A extends BasicFileAttributes> A readAttributes(Path path,Class<A> type,LinkOption... options) : 获取与 path 指定的文件相关联的属性。
 45      */
 46     
 47     @Test
 48     public void test6() throws IOException{
 49         Path path = Paths.get("1.jpg");
 50 //        System.out.println(Files.exists(path, LinkOption.NOFOLLOW_LINKS));
 51         
 52         BasicFileAttributes readAttributes = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
 53         System.out.println(readAttributes.creationTime());
 54         System.out.println(readAttributes.lastModifiedTime());
 55         
 56         DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
 57         
 58         fileAttributeView.setHidden(false);
 59     }
 60     
 61     /*
 62     Files常用方法:
 63         Path copy(Path src, Path dest, CopyOption … how) : 文件的复制
 64         Path createDirectory(Path path, FileAttribute<?> … attr) : 创建一个目录
 65         Path createFile(Path path, FileAttribute<?> … arr) : 创建一个文件
 66         void delete(Path path) : 删除一个文件
 67         Path move(Path src, Path dest, CopyOption…how) : 将 src 移动到 dest 位置
 68         long size(Path path) : 返回 path 指定文件的大小
 69      */
 70     
 71     @Test
 72     public void test5() throws IOException{
 73         Path path2 = Paths.get("E:/jucAndnioCode/NIO/1.jpg");
 74         
 75         System.out.println(Files.size(path2));
 76         
 77 //        Files.move(path1, path2, StandardCopyOption.ATOMIC_MOVE);
 78     }
 79     
 80     @Test
 81     public void test4() throws IOException{
 82         Path dir = Paths.get("e:/nio/nio2");
 83 //        Files.createDirectory(dir);
 84         
 85         Path file = Paths.get("E:/jucAndnioCode/NIO/1.jpg");
 86 //        Files.createFile(file);
 87         
 88         Files.deleteIfExists(file);
 89     }
 90     
 91     @Test
 92     public void test3() throws IOException{
 93         Path path1 = Paths.get("1.jpg");
 94         Path path2 = Paths.get("4.jpg");
 95         
 96         Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
 97     }
 98     
 99     /*
100     Paths 提供的 get() 方法用来获取 Path 对象:
101         Path get(String first, String … more) : 用于将多个字符串串连成路径。
102     Path 常用方法:
103         boolean endsWith(String path) : 判断是否以 path 路径结束
104         boolean startsWith(String path) : 判断是否以 path 路径开始
105         boolean isAbsolute() : 判断是否是绝对路径
106         Path getFileName() : 返回与调用 Path 对象关联的文件名
107         Path getName(int idx) : 返回的指定索引位置 idx 的路径名称
108         int getNameCount() : 返回Path 根目录后面元素的数量
109         Path getParent() :返回Path对象包含整个路径,不包含 Path 对象指定的文件路径
110         Path getRoot() :返回调用 Path 对象的根路径
111         Path resolve(Path p) :将相对路径解析为绝对路径
112         Path toAbsolutePath() : 作为绝对路径返回调用 Path 对象
113         String toString() : 返回调用 Path 对象的字符串表示形式
114  */
115     
116     @Test
117     public void test2(){
118         Path path = Paths.get("e:/nio/hello.txt");
119         
120         System.out.println(path.getParent());
121         System.out.println(path.getRoot());
122         
123 //        Path newPath = path.resolve("e:/hello.txt");
124 //        System.out.println(newPath);
125         
126         Path path2 = Paths.get("1.jpg");
127         Path newPath = path2.toAbsolutePath();
128         System.out.println(newPath);
129         
130         System.out.println(path.toString());
131     }
132     
133     @Test
134     public void test1(){
135         Path path = Paths.get("e:/", "nio/hello.txt");
136         
137         System.out.println(path.endsWith("hello.txt"));
138         System.out.println(path.startsWith("e:/"));
139         
140         System.out.println(path.isAbsolute());
141         System.out.println(path.getFileName());
142         
143         for (int i = 0; i < path.getNameCount(); i++) {
144             System.out.println(path.getName(i));
145         }
146     }
147 }

 

posted @ 2017-08-26 16:45  白日梦想家12138  阅读(219)  评论(0编辑  收藏  举报