java中的Path和Files之Files
Files 读写文件
- 快速读取文件。
byte[] bytes = Files.readAllBytes(path);
- 将文件当作字符串读入,那么可以在调用 readAllBytes 之后执行。
String content = new String(bytes, charset);
- 将文件当作行序列读入。
List<String> lines = Files.readAllLines(path,charset);
- 将字符串写入到一个文件中。
Files.write(path, content.getBytes(charset));
- 向指定文件追加内容。
Files.write(pathh,content.getBytes(charset),StandardOpenOption.APPEND);
- 将一个行的集合写入到文件中。
Files.write(path,lines);
上面方法适合中等长度的文件,如果处理的文件长度较大,或者是二进制文件,就得用输入/输出流了。
InputStream in = Files.newInputStream(path);
OutputStream out = Files.newOutputStream(path);
Reader reader = Files.newBufferedReader(path,charset);
Writer writer = Files.newBufferedWriter(path,charset);
创建文件和目录
- 创建目录。
Files.createDirectory(path);
路径除最后一个部件外,其他部分都必须是已存在的,如果要创建中间的目录应该用
Files.createDirectories(path);
- 创建一个空文件。
Files.createFile(path);
如果文件已经存在,那么调用这个方法就会抛出异常,检查文件的存在与否与创建文件应该是原子性的,如果文件不存在,该文件就会被创建,并且其他程序在此过程中是无法执行文件创建操作的。
- 给定位置创建临时文件或临时目录。
Path p = Files.createTempFile(dir,prefix,suffix);
Path p = Files.createTempFile(prefix,suffix);
Path p = Files.createTempDirectory(dir,prefix);
Path p = Files.createTempDirectory(prefix);
dir 是一个 Path 对象, prefix 和 suffix 是可以为 null 的字符串。如:
Files.createTempFile(null,".txt");
复制、移动和删除文件
- 复制文件。
Files.copy(fromPath, toPath);
- 移动文件。
Files.remove(fromPath, toPath);
上述方法如果目标路径已经存在,则复制或者移动就会失败。可以使用下面的方法:
- 复制并替换掉原来的文件(若存在)。
Files.copy(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
- 复制文件并复制文件的属性。
Files.copy(fromPath, toPath, StandardCopyOption.COPY_ATTRIBUTES);
- 原子性的移动文件(要么移动成功,要么移动失败原文件继续保留在原来的位置)。
Files.move(fromPath, toPath, StandardCopyOption.ATOMIC_MOVE);
- 将一个输入流复制到 Path 。
Files.copy(inputStream, toPath);
- 将 Path 复制到输出流中。
Files.copy(fromPath, outputStream);
- 删除文件。
Files.delete(path);
如果删除的文件不存在会抛出异常,因此可以使用下面的方法:
- 判断文件是否存在并删除,此方法也可以用来删除空目录。
boolean deleted = Files.deleteIfExists(path);
附1 StandardOpenOption 与 newInputStream,newOutputStream,newBufferedWriter,write 结合使用。
InputStream in = Files.newInputStream(path, StandardOpenOption.READ);
OutputStream out = Files.newOutputStream(path,StandardOpenOption.WRITE);
Writer writer = Files.newBufferedWriter(path,StandardCharsets.UTF_8,StandardOpenOption.WRITE);
Files.write(path, bytes, StandardOpenOption.SPARSE);
- READ
用于读取文件而打开。
- WRITE
用于写入文件而打开。
- APPEND
用于写入文件,在文件末尾追加内容。
- TRUNCATE_EXISTING
用于写入文件,移除已有内容。
- CREATE_NEW
创建新文件,并且在已存在的情况下会创建失败。
- CREATE
自动在文件不存在的情况下创建新文件。
- DELETE_ON_CLOSE
当文件被关闭时,尽可能的删除该文件。
- SPARSE
创建稀疏文件。
- SYNC
对文件或元数据的每次更新都同步写入底层设备
- DSYNC
只更新文件内容每次同步到底层设备
附2 StandardCopyOption 与copy、 move 一起使用及StandardCopyOption 类的源码:
public enum StandardCopyOption implements CopyOption {
/**
* Replace an existing file if it exists.
* 如果目标已存在,则替换它。
*/
REPLACE_EXISTING,//
/**
* Copy attributes to the new file.
* 复制文件的属性。
*/
COPY_ATTRIBUTES,
/**
* Move the file as an atomic file system operation.
* 原子怕的移动文件。
*/
ATOMIC_MOVE;
}
附3 LinkOption 类的源码:
public enum LinkOption implements OpenOption, CopyOption {
/**
* Do not follow symbolic links.
*
* @see Files#getFileAttributeView(Path,Class,LinkOption[])
* @see Files#copy
* @see SecureDirectoryStream#newByteChannel
* 不要跟踪符号链接
*/
NOFOLLOW_LINKS;
}
附 4 FileVisitOption 类的源码:
public enum FileVisitOption {
/**
* Follow symbolic links.
* 跟踪符号链接
*/
FOLLOW_LINKS;
}