java 文件路径操作

本文主要介绍了Java中关于文件路径的各种操作,包括如何获取路径、构造路径以及判断文件路径的有效性。通过实例演示,详细解释了相关API的使用方法。
摘要由CSDN通过智能技术生成

获取路径、构造、判断文件路径

实例:

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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值