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

推荐订阅源

V
Vulnerabilities – Threatpost
V
V2EX
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
Y
Y Combinator Blog
C
Check Point Blog
爱范儿
爱范儿
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
雷峰网
雷峰网
IT之家
IT之家
WordPress大学
WordPress大学
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
罗磊的独立博客
S
SegmentFault 最新的问题
S
Security Affairs
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Hacker News: Front Page
Google DeepMind News
Google DeepMind News
B
Blog
O
OpenAI News
C
Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
Hacker News: Ask HN
Hacker News: Ask HN
博客园_首页
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Help Net Security
Help Net Security
月光博客
月光博客
J
Java Code Geeks
L
LangChain Blog
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Apple Machine Learning Research
Apple Machine Learning Research
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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

用一个实际业务场景演示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;
    }

--