一丶文件操作NIO Files的简单使用
Path path = Paths.get("E:\\FileDownload\\FTPFile\\newPDF");
Path source = Paths.get("E:\\FileDownload\\FTPFile\\text.txt");
Path destination = Paths.get("E:\\FileDownload\\FTPFile\\newPDF\\text.txt");
1丶创建所有目录
Path path1 = Files.createDirectories(path);
System.out.println(path1);
2丶判断文件/目录是否存在
boolean exists = Files.exists(path);
System.out.println(exists);
3丶创建文件
// 此处需判断文件是否存在, 已存在, 会报错, 将无法创建
Path source1 = Files.createFile(source);
Path destination1 = Files.createFile(destination);
System.out.println(source1);
System.out.println(destination1);
4丶写入内容
Path destination2 = Files.write(source, "Hello World !".getBytes());
System.out.println(destination2);
5丶读取文件内容
List<String> list2 = Files.readAllLines(source);
System.out.println(JSONObject.toJSON(list2));
6丶删除文件/目录
// 如果存在则执行删除
Files.deleteIfExists(destination);
7丶复制文件
// 一参为需复制的文件, 二参为复制到目标文件路径, 三参为类型(文件存在则替换)
Path destination3 = Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
System.out.println(destination3);
8丶获取文件大小
long size = Files.size(destination);
System.out.println(size);
9丶移动文件
// 一参为需移动的文件, 二参为移动到目标文件路径, 三参为类型(文件存在则替换)
Path destination4 = Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
System.out.println(destination4);