惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

博客园 - 有点懒惰的大青年

shell脚本变量记录 Python格式化字符串 Python中的浅拷贝和深拷贝 CompletableFuture.runAsync来进行异步任务和ThreadPoolExecutor来执行有什么区别? 用一个实际业务场景演示ON和WHERE的正确使用方式 idea中查看源码 异或运算 使用Hutool对时间的花式操作 合并K个升序列表 比较器 枚举类的设计模式 spring boot中使用RedissonClient实现分布式锁 idea同时启动application,启用不同端口 @PostConstruct用法 ApplicationContext 事件发布与监听机制详解 java自带命令jps介绍 关于maven中标签的说明 mysql的跨库查询 linux命令ll显示结果的含义 java环境变量设置 java值传递和引用传递 kafka的集群与可靠性 kafka的消费全流程 关于kafka 达梦数据库执行计划介绍 关于MQ 用位运算实现加减乘除(3)
Files类的使用
有点懒惰的大青年 · 2025-10-31 · via 博客园 - 有点懒惰的大青年

java nio包中Files类的使用,从jdk1.7引入的,下面是简单使用例子说明。

1.判断文件是否存在、拷贝文件

package com.test.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class TestCopyFile {

    public static void main(String[] args) {
        String source = "D:\\freestyle\\SJZT\\ODS\\copytest.txt";
        String dest = "D:\\freestyle\\share_files\\download\\bankStaffInfo\\copytest.txt";

        boolean b1 = new File(source).exists();
        System.out.println("文件是否存在:" + b1);

        /*
         * 判断文件是否存在
         *
         * LinkOption可以不传,它代表的是"链接选项"
         * 这个参数主要用于指示方法在检查文件是否存在时,如何处理符号链接。它是一个特殊的文件,指向另一个文件或目录。
         * 如果不传linkOption则默认是跟踪符号链接的。它检查的不是符号链接文件本身是否存在,而是它所指向的目标是否存在。
         * LinkOption只有一个枚举值:NOFOLLOW_LINKS,表示不跟随符号链接。它只检查给定的路径符号链接文件本身是否存在,而完全不关心它指向什么。
         *
         * 补充:
         * 非符号链接文件:如果你检查的路径不是一个符号链接,而是一个普通文件或目录,那么无论是否传递 LinkOption.NOFOLLOW_LINKS,
         * 结果都是一样的,都是检查该普通文件或目录本身是否存在。
         *
         */
        boolean b2 = Files.exists(Paths.get(source));
        System.out.println("文件是否存在:" + b2);

        boolean success = copy(source, dest);
        System.out.println("文件拷贝结果:" + success);
    }


    /**
     * 拷贝文件
     *
     * @param sourcePath
     * @param destPath
     * @return
     */
    public static boolean copy(String sourcePath, String destPath) {
        try {
            Path source = Paths.get(sourcePath);
            Path destination = Paths.get(destPath);
            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

上面Files.copy如果目标文件的父路径文件夹不存在时,会报错,优化成如下:

/**
     * 拷贝文件
     *
     * @param sourcePath
     * @param destPath
     * @return
     */
    public static boolean copy(String sourcePath, String destPath) {
        try {
            Path source = Paths.get(sourcePath);
            Path destination = Paths.get(destPath);
            Path parentDestination = destination.getParent();
            if (parentDestination != null && !Files.exists(parentDestination)) {
                Files.createDirectories(parentDestination);
                System.out.println("directory not exist, create:" + parentDestination + " success");
            }

            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

--