获取路径、构造、判断文件路径
实例:
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
class FileDirTest_3_3 {
public static void main(String[] args){
// 获取用户工作目录
String userDir = System.getProperty("user.dir");
System.out.println("userDir = " + userDir);
// 路径构造
// 简单Path构造
// 相对路径表示法
Path path = Paths.get("test.txt");
System.out.println("Path = " + path);
// 转换成绝对路径看下究竟,可以看到是相对于“工作路径”来计算的
System.out.println("absPath = " + path.toAbsolutePath());
System.out.println("workingDir = " + System.getProperty("user.dir"));
// 绝对路径表示法(包含根)
path = Paths.get("\\test.txt");
System.out.println("path = " + path);
// 转换成绝对路径看下究竟,可以看到没有变化
System.out.println("absPath = " + path.toAbsolutePath());
System.out.println("path = " + Paths.get("D:", "test.txt"));
System.out.println("path = " + Paths.get("D:", "test.txt").toAbsolutePath());
// 复杂Path构造
Path path1 = Paths.get("doc", "test.txt");
System.out.println("path = " + path1);
System.out.println("absPath = " +path1.toAbsolutePath());
// 路径解析,,Path 对象解析
System.out.println("路径解析,path对象解析: ");
Path path2 = Paths.get("doc", "chapter1", "test.txt");
String pathstr = path2.toString();
System.out.println("pathStr = " + pathstr);
Path absPath = path2.toAbsolutePath(); // absPath = D:\projects\ideaProjects\component1\doc\chapter1\test.txt
System.out.println("absPath = " + absPath);
Path fileName = path2.getFileName();
System.out.println("fileName = " + fileName); // fileName = test.txt
// 输出路径的每个子目录
int nameCount = path2.getNameCount();
for (int i = 0; i < nameCount; i++){
System.out.println("name(" + i + ") = " + path2.getName(i));
}
// 判断是否为目录
String dirPath = "D:\\projects\\ideaProjects\\component1\\src\\main\\java\\chapter2";
Path path3 = Paths.get(dirPath);
System.out.println("判断是否是目录: " + Files.isDirectory(path3));
// 判断是否为文件
Path path4 = Paths.get("D:\\projects\\ideaProjects\\component1\\src\\main\\java\\chapter3\\FileDirTest_3_3.java");
System.out.println("判断是否是文件: " + Files.isRegularFile(path4));
System.out.println("判断是否是文件: " + Files.isRegularFile(path3));
// 查找目录,不存在则创建一个新的
String testPath = "D:\\projects\\ideaProjects\\component1\\src\\main\\java\\chapter4";
Path path5 = getOrMakeDir(testPath);
System.out.println("查找目录,不存在则创建一个新的: " + path5);
// 列举目录下所有文件(不包含子文件夹)
String dir = "D:\\projects\\ideaProjects\\component1\\src\\main\\java\\testDir";
// List<File> fileList = (List<File>)FileUtils.listFiles(dir,null,false);
// System.out.println(PathUtils.listFiles("D:\\projects\\ideaProjects\\component1\\src\\main\\java\\chapter3"));
File dirPath1 = new File(dir);
String[] children = dirPath1.list();
System.out.println(Arrays.toString(children));
for (String s : children){
System.out.println(s);
}
}
// 查找目录,不存在则创建一个新的
private static Path getOrMakeDir(String location){
Path path = Paths.get(location);
if (Files.exists(path)){
if (!Files.isDirectory(path)){
return null;
}
return path;
}
try {
Files.createDirectory(path);
} catch (IOException e){
return null;
}
return path;
}
private static Stream<Path> walk(String location, boolean followLink, boolean recursion)
throws IOException {
Path path = Paths.get(location);
if (!Files.exists(path)) {
return Stream.empty();
}
int depth = recursion ? Integer.MAX_VALUE : 1;
return followLink ? Files.walk(path, depth, FileVisitOption.FOLLOW_LINKS) :
Files.walk(path, depth);
}
}