Files是 Java NIO (New I/O) 包中的一个工具类,提供了一些静态方法用于对文件和文件系统进行常见的操作,如:文件的读取、写入、复制和移动等。这些方法简化了文件操作的复杂性,比 java.io.File 提供的方法更加高效,并且能够更好地与 NIO 的 Path、FileSystem 等功能协同工作。
一、文件和目录创建
1.createFile
格式:createFile(Path path)
创建一个新的空文件,如果文件已经存在,则会抛出 FileAlreadyExistsException异常。
Files.createFile(Path.of("example.txt")); |
2.createDirectory
格式:createDirectory(Path path)
创建一个目录,如果目录已存在则会抛出异常。
Files.createDirectory(Path.of("exampleDir")); |
3.createDirectories
格式:createDirectories(Path path)
创建目录,如果父目录不存在,也会创建父目录。
Files.createDirectories(Path.of("exampleDir/subdir")); |
二、文件删除
1.delete
格式:delete(Path path)
删除指定的文件,如果文件不存在或者不是文件,会抛出异常。
Files.delete(Path.of("example.txt")); |
2.deleteIfExists
格式:deleteIfExists(Path path)
删除文件,如果文件不存在,不会抛出异常。
Files.deleteIfExists(Path.of("example.txt")); |
三、文件拷贝
1.copy
格式:copy(Path source, Path target, CopyOption... options)
将源文件复制到目标路径,支持多种复制选项(例如:覆盖、复制符号链接等)
Path source = Path.of("source.txt"); Path target = Path.of("target.txt"); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); |
四、文件移动
1.move
格式:move(Path source, Path target, CopyOption... options)
将源文件移动到目标路径。可以指定是否覆盖目标文件。
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); |
五、文件读取和写入
1.readAllBytes
格式:readAllBytes(Path path)
读取文件的所有字节内容
Path path = Path.of("file.txt"); byte[] bytes = Files.readAllBytes(path); |
2.readAllLines
格式:readAllLines(Path path)
读取文件的所有行,返回一个 List<String>,每行是一个字符串。
List<String> lines = Files.readAllLines(Path.of("example.txt")); |
六、文件写入
1.write
格式:write(Path path, byte[] bytes, OpenOption... options)
将字节数组写入文件。如果文件不存在,则会创建文件。如果文件存在,可以选择覆盖或追加内容。
Path path = Path.of("file.txt"); byte[] bytes = "Hello, World!".getBytes(); Files.write(path, bytes, StandardOpenOption.CREATE); |
格式:write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options)
将字符串集合(如 List<String>)写入文件,每个元素作为一行。可以指定字符集。
Path path = Path.of("file.txt"); List<String> lines = Arrays.asList("Line 1", "Line 2"); Files.write(path, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE); |
七、文件属性
1.getLastModifiedTime
格式:getLastModifiedTime(Path path)
获取文件的最后修改时间,返回一个 FileTime 对象。
2.getSize
格式:getSize(Path path)
获取文件的大小,以字节为单位。
3.setLastModifiedTime
格式:setLastModifiedTime(Path path, FileTime time)
设置文件的最后修改时间。
Path path = Path.of("file.txt"); FileTime fileTime = Files.getLastModifiedTime(path); long size = Files.size(path); Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis())); |
八、目录遍历
1.walk
格式:walk(Path start, FileVisitOption... options)
遍历指定目录及其子目录,返回一个 Stream<Path>,适合处理大文件树。
try (Stream<Path> paths = Files.walk(Path.of("dir"))) { paths.forEach(System.out::println);} |
2.list
格式:list(Path dir)
列出目录下的所有文件和子目录,不递归遍历子目录。
try (Stream<Path> paths = Files.list(Path.of("dir"))) { paths.forEach(System.out::println);} |
九、符号链接
1.createSymbolicLink
格式:createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
创建符号链接,link 是符号链接的路径,target 是目标文件或目录的路径。
Files.createSymbolicLink(source, target); |
2.isSymbolicLink
格式:isSymbolicLink(Path path)
判断文件是否为符号链接。
boolean isLink = Files.isSymbolicLink(Path.of("link.txt")); |
十、POSIX 文件权限
1.setPosixFilePermissions
格式:setPosixFilePermissions(Path path, Set<PosixFilePermission> perms)
设置文件的 POSIX 权限,仅在支持 POSIX 权限的操作系统(如类 Unix 系统)有效。
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r--r--"); Files.setPosixFilePermissions(Path.of("file.txt"), perms); |
十一、文件比较
1.isSameFile
格式:isSameFile(Path path1, Path path2)
比较两个文件是否为同一个文件,返回布尔值。
boolean isSame = Files.isSameFile(source, target); |
十二、文件信息
1.probeContentType
格式:probeContentType(Path path)
获取文件的内容类型(MIME 类型)。
String contentType = Files.probeContentType(Path.of("file.txt")); |
2.exists
格式:exists(Path path, LinkOption... options)
判断文件是否存在,可以指定是否检查符号链接的目标。
boolean exists = Files.exists(Path.of("file.txt"), LinkOption.NOFOLLOW_LINKS); |