Path、Paths 和 Files工具类的使用

一:创建文件或目录。

/**
 * 创建文件或目录
 */
private static void createFileOrDir() {
    try {
        // 创建新目录,除了最后一个部件,其他必须是存在的
        Files.createDirectory(Paths.get("F:/test"));

        // 创建路径中的中间目录,能创建不存在的中间部件
        Files.createDirectories(Paths.get("F:/test/test"));

        // 创建文件及相关目录
        Path path = Paths.get("F:/test/testbak.txt");
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

二:删除文件

/**
 * 删除文件
 */
private static void deleteFile() {
    Path p = Paths.get("F:/test.txt");
    try {
        Files.delete(p);// 用static boolean deleteIfExists(Path path)方法比较好
        System.out.println("删除成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

三:移动文件

/**
 * 移动文件
 */
private static void moveFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("E:/test.txt");
    try {
        Files.move(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("移动成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

四:复制文件

/**
 * 复制文件
 */
private static void copyFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("F:/testbak.txt");
    try {
        Files.copy(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("复制成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

五:获取文件字节

/**
 * 从文件读取数据
 */
private static void readFromFile() {
    Path p = Paths.get("F:/", "test.txt");
    try {
        byte[] bytes = Files.readAllBytes(p);
        System.out.println(new String(bytes));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

六:按行读取文件

/**
 * 从文件按行读取数据
 */
private static void readByLineFromFile() {
    Path path = Paths.get("F:/", "test.txt");
    try {
    	// 方式1
        List<String> lines = Files.lines(path).collect(Collectors.toList());
        System.out.println(lines);
        
        // 方式2
        BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        String str = null;
        while((str = reader.readLine()) != null){
            System.out.println(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

七:向文件写入数据

/**
 * 向文件写入数据
 */
private static void write2File() {
    // 获得路径
    Path path = Paths.get("F:/", "test.txt");
    String info = "I love java really,你喜欢什么?";
    try {
        // 方式1
        Files.write(path, info.getBytes("utf8"), StandardOpenOption.APPEND);

		// 方式2
        BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
        writer.write(info );
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

八:获取文件路径

/**
 * 获得文件路径的几种方法
 */
private static void getFilePath() {
    File file = new File("F:/test.txt");
    // Path
    Path p1 = Paths.get("F:/", "test.txt");// F:\test.txt
    System.out.println(p1);

    Path p2 = file.toPath();
    System.out.println(p2);

    Path p3 = FileSystems.getDefault().getPath("F:/", "test.txt");
    System.out.println(p3);
}

转载地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值