操作文件和目录
1.建立文件或目录
1 import java.nio.file.Files; 2 import java.nio.file.Path; 3 import java.nio.file.Paths; 4 import java.nio.file.attribute.FileAttribute; 5 import java.nio.file.attribute.PosixFilePermission; 6 import java.nio.file.attribute.PosixFilePermissions; 7 import java.util.Set; 8 9 public class FileDemo { 10 public static void main(String args[]) { 11 Path p=Paths.get("G:\学习型文档\java_test\Myfile.txt"); 12 //创建泛型集合 13 //Set<PosixFilePermission> perms=PosixFilePermissions.fromString("rw-rw-rw-"); 14 //ileAttribute <Set<PosixFilePermission>> attr=PosixFilePermissions.asFileAttribute(perms); 15 16 try { 17 Files.createFile(p); 18 } 19 catch(Exception e) { 20 System.out.println("文件已经存在!"); 21 } 22 23 24 } 25 }
测试结果:
2.复制文件或目录
在1的基础上执行:
1 import java.nio.file.Files; 2 import java.nio.file.Path; 3 import java.nio.file.Paths; 4 import java.nio.file.attribute.FileAttribute; 5 import java.nio.file.attribute.PosixFilePermission; 6 import java.nio.file.attribute.PosixFilePermissions; 7 import java.util.Set; 8 9 public class FileDemo { 10 public static void main(String args[]) { 11 Path p=Paths.get("G:\学习型文档\java_test\Myfile.txt"); 12 Path PP=Paths.get("G:\学习型文档\java_test\Myfile1.txt"); 13 //创建泛型集合 14 //Set<PosixFilePermission> perms=PosixFilePermissions.fromString("rw-rw-rw-"); 15 //ileAttribute <Set<PosixFilePermission>> attr=PosixFilePermissions.asFileAttribute(perms); 16 17 try { 18 Files.copy(p,PP); 19 } 20 catch(Exception e) { 21 System.out.println("文件已经存在!"); 22 } 23 24 25 } 26 }
测试结果:
3.移动文件或目录;
1 import java.nio.file.Files; 2 import java.nio.file.Path; 3 import java.nio.file.Paths; 4 import java.nio.file.attribute.FileAttribute; 5 import java.nio.file.attribute.PosixFilePermission; 6 import java.nio.file.attribute.PosixFilePermissions; 7 import java.util.Set; 8 9 public class FileDemo { 10 public static void main(String args[]) { 11 Path p=Paths.get("G:\学习型文档\java_test\Myfile.txt"); 12 Path PP=Paths.get("G:\学习型文档\java_test\Myfile1.txt"); 13 Path PPP=Paths.get("G:\学习型文档\java_test\Myfile2.txt"); 14 //创建泛型集合 15 //Set<PosixFilePermission> perms=PosixFilePermissions.fromString("rw-rw-rw-"); 16 //ileAttribute <Set<PosixFilePermission>> attr=PosixFilePermissions.asFileAttribute(perms); 17 18 try { 19 Files.move(p,PPP); 20 } 21 catch(Exception e) { 22 System.out.println("文件已经存在!"); 23 } 24 25 26 } 27 }
测试结果:
4.遍历文件树:
1 import java.io.IOException; 2 import java.nio.file.FileVisitResult; 3 import java.nio.file.FileVisitor; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 import java.nio.file.attribute.BasicFileAttributes; 8 9 class MyFileVisitor implements FileVisitor<Path> { 10 11 public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { 12 System.out.println("Just Visited" + dir); 13 return FileVisitResult.CONTINUE; 14 } 15 16 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 17 System.out.println("About to visit" + dir); 18 return FileVisitResult.CONTINUE; 19 } 20 21 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 22 System.out.println("Currently visiting " + file); 23 System.out.println("Is this file a directory:" + Files.isDirectory(file)); 24 System.out.println("Checking done...!!"); 25 return FileVisitResult.CONTINUE; 26 } 27 28 public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException { 29 System.err.println(e.getMessage()); 30 return FileVisitResult.CONTINUE; 31 } 32 } 33 34 public class NIODemo { 35 public static void main(String args[]) throws IOException { 36 Path listDir = Paths.get("G:\学习型文档\C_矩阵"); 37 MyFileVisitor obj = new MyFileVisitor(); 38 Files.walkFileTree(listDir, obj); 39 40 } 41 }
此代码为遍历 G:\学习型文档\C_矩阵 该文件下的文件树:
测试结果: