阅读,编写和创建文件

原文: https://docs.oracle.com/javase/tutorial/essential/io/file.html

本页讨论了阅读,编写,创建和打开文件的详细信息。有多种文件 I / O 方法可供选择。为了帮助理解 API,下图按复杂性排列文件 I / O 方法。

Line drawing with file I/O methods arranged from least complex (on the left) to most complex (on the right).

File I/O Methods Arranged from Less Complex to More Complex

图的最左侧是实用方法readAllBytesreadAllLineswrite方法,专为简单的常见情况而设计。在这些的右边是用于迭代一行或多行文本的方法,例如newBufferedReadernewBufferedWriter,然后是newInputStreamnewOutputStream。这些方法可与java.io包互操作。在这些方法的右边是处理ByteChannelsSeekableByteChannelsByteBuffers的方法,例如newByteChannel方法。最后,在最右边的是使用FileChannel的方法,用于需要文件锁定或内存映射 I / O 的高级应用程序。


Note: The methods for creating a new file enable you to specify an optional set of initial attributes for the file. For example, on a file system that supports the POSIX set of standards (such as UNIX), you can specify a file owner, group owner, or file permissions at the time the file is created. The Managing Metadata page explains file attributes, and how to access and set them.


此页面包含以下主题:


本节中的几个方法采用可选的OpenOptions参数。此参数是可选的,API 会告诉您在未指定方法时该方法的默认行为。

支持以下StandardOpenOptions枚举:

  • WRITE - 打开文件以进行写访问。
  • APPEND - 将新数据追加到文件末尾。此选项与WRITECREATE选项一起使用。
  • TRUNCATE_EXISTING - 将文件截断为零字节。此选项与WRITE选项一起使用。
  • CREATE_NEW - 如果文件已存在,则创建一个新文件并抛出异常。
  • CREATE - 打开文件(如果存在)或创建新文件(如果不存在)。
  • DELETE_ON_CLOSE - 关闭流时删除文件。此选项对临时文件很有用。
  • SPARSE - 提示新创建的文件将是稀疏的。此高级选项在某些文件系统(例如 NTFS)上受到尊重,其中具有数据“间隙”的大文件可以以更有效的方式存储,其中这些空间隙不占用磁盘空间。
  • SYNC - 使文件(内容和元数据)与底层存储设备保持同步。
  • DSYNC - 使文件内容与底层存储设备保持同步。

如果你有一个小文件,并且想要一次读取它的全部内容,你可以使用 readAllBytes(Path)readAllLines(Path, Charset) 方法。这些方法可以为您完成大部分工作,例如打开和关闭流,但不用于处理大型文件。以下代码显示了如何使用readAllBytes方法:

  1. Path file = ...;
  2. byte[] fileArray;
  3. fileArray = Files.readAllBytes(file);

您可以使用其中一种写入方法将字节或行写入文件。

以下代码段显示了如何使用write方法。

  1. Path file = ...;
  2. byte[] buf = ...;
  3. Files.write(file, buf);

java.nio.file软件包支持通道 I / O,它可以移动缓冲区中的数据,绕过一些可能会阻塞流 I / O 的层。

newBufferedReader(Path, Charset) 方法打开一个文件进行读取,返回一个BufferedReader,可以用来以有效的方式从文件中读取文本。

以下代码段显示了如何使用newBufferedReader方法从文件中读取。该文件以“US-ASCII”编码。

  1. Charset charset = Charset.forName("US-ASCII");
  2. try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
  3. String line = null;
  4. while ((line = reader.readLine()) != null) {
  5. System.out.println(line);
  6. }
  7. } catch (IOException x) {
  8. System.err.format("IOException: %s%n", x);
  9. }

您可以使用 newBufferedWriter(Path, Charset, OpenOption...) 方法使用BufferedWriter写入文件。

以下代码段显示了如何使用此方法创建以“US-ASCII”编码的文件:

  1. Charset charset = Charset.forName("US-ASCII");
  2. String s = ...;
  3. try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
  4. writer.write(s, 0, s.length());
  5. } catch (IOException x) {
  6. System.err.format("IOException: %s%n", x);
  7. }

要打开文件进行读取,可以使用 newInputStream(Path, OpenOption...) 方法。此方法返回一个无缓冲的输入流,用于从文件中读取字节。

  1. Path file = ...;
  2. try (InputStream in = Files.newInputStream(file);
  3. BufferedReader reader =
  4. new BufferedReader(new InputStreamReader(in))) {
  5. String line = null;
  6. while ((line = reader.readLine()) != null) {
  7. System.out.println(line);
  8. }
  9. } catch (IOException x) {
  10. System.err.println(x);
  11. }

您可以使用 newOutputStream(Path, OpenOption...) 方法创建文件,附加到文件或写入文件。此方法打开或创建用于写入字节的文件,并返回无缓冲的输出流。

该方法采用可选的OpenOption参数。如果未指定打开选项,并且该文件不存在,则会创建一个新文件。如果文件存在,则会被截断。此选项相当于使用CREATETRUNCATE_EXISTING选项调用方法。

以下示例打开一个日志文件。如果该文件不存在,则创建该文件。如果该文件存在,则打开该文件以进行追加。

  1. import static java.nio.file.StandardOpenOption.*;
  2. import java.nio.file.*;
  3. import java.io.*;
  4. public class LogFileTest {
  5. public static void main(String[] args) {
  6. // Convert the string to a
  7. // byte array.
  8. String s = "Hello World! ";
  9. byte data[] = s.getBytes();
  10. Path p = Paths.get("./logfile.txt");
  11. try (OutputStream out = new BufferedOutputStream(
  12. Files.newOutputStream(p, CREATE, APPEND))) {
  13. out.write(data, 0, data.length);
  14. } catch (IOException x) {
  15. System.err.println(x);
  16. }
  17. }
  18. }

的方法

当流 I / O 一次读取一个字符时,通道 I / O 一次读取一个缓冲区。 ByteChannel 接口提供基本的readwrite功能。 SeekableByteChannelByteChannel,能够维持通道中的位置并改变该位置。 SeekableByteChannel还支持截断与通道关联的文件并查询文件的大小。

移动到文件中的不同点然后从该位置读取或写入的能力使得可以随机访问文件。有关详细信息,请参阅随机存取文件

读取和写入通道 I / O 有两种方法。


Note: The newByteChannel methods return an instance of a SeekableByteChannel. With a default file system, you can cast this seekable byte channel to a FileChannel providing access to more advanced features such mapping a region of the file directly into memory for faster access, locking a region of the file so other processes cannot access it, or reading and writing bytes from an absolute position without affecting the channel’s current position.


两种newByteChannel方法都可以指定OpenOption选项列表。除了另外一个选项外,还支持newOutputStream方法使用的相同打开选项READ是必需的,因为SeekableByteChannel支持读取和写入。

指定READ将打开通道进行读取。指定WRITEAPPEND将打开通道进行写入。如果未指定这些选项,则打开通道进行读取。

以下代码段读取文件并将其打印到标准输出:

  1. // Defaults to READ
  2. try (SeekableByteChannel sbc = Files.newByteChannel(file)) {
  3. ByteBuffer buf = ByteBuffer.allocate(10);
  4. // Read the bytes with the proper encoding for this platform. If
  5. // you skip this step, you might see something that looks like
  6. // Chinese characters when you expect Latin-style characters.
  7. String encoding = System.getProperty("file.encoding");
  8. while (sbc.read(buf) > 0) {
  9. buf.rewind();
  10. System.out.print(Charset.forName(encoding).decode(buf));
  11. buf.flip();
  12. }
  13. } catch (IOException x) {
  14. System.out.println("caught exception: " + x);

以下示例是为 UNIX 和其他 POSIX 文件系统编写的,它创建了一个具有一组特定文件权限的日志文件。此代码创建日志文件或附加到日志文件(如果已存在)。创建日志文件时,对所有者具有读/写权限,对组具有只读权限。

  1. import static java.nio.file.StandardOpenOption.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. import java.nio.file.*;
  5. import java.nio.file.attribute.*;
  6. import java.io.*;
  7. import java.util.*;
  8. public class LogFilePermissionsTest {
  9. public static void main(String[] args) {
  10. // Create the set of options for appending to the file.
  11. Set<OpenOption> options = new HashSet<OpenOption>();
  12. options.add(APPEND);
  13. options.add(CREATE);
  14. // Create the custom permissions attribute.
  15. Set<PosixFilePermission> perms =
  16. PosixFilePermissions.fromString("rw-r-----");
  17. FileAttribute<Set<PosixFilePermission>> attr =
  18. PosixFilePermissions.asFileAttribute(perms);
  19. // Convert the string to a ByteBuffer.
  20. String s = "Hello World! ";
  21. byte data[] = s.getBytes();
  22. ByteBuffer bb = ByteBuffer.wrap(data);
  23. Path file = Paths.get("./permissions.log");
  24. try (SeekableByteChannel sbc =
  25. Files.newByteChannel(file, options, attr)) {
  26. sbc.write(bb);
  27. } catch (IOException x) {
  28. System.out.println("Exception thrown: " + x);
  29. }
  30. }
  31. }

您可以使用 createFile(Path, FileAttribute&lt;?&gt;) 方法创建具有初始属性集的空文件。例如,如果在创建时希望文件具有特定的文件权限集,请使用createFile方法执行此操作。如果未指定任何属性,则使用默认属性创建文件。如果文件已存在,createFile将引发异常。

在单个原子操作中,createFile方法检查文件是否存在,并使用指定的属性创建该文件,这使得该过程对恶意代码更安全。

以下代码段创建一个具有默认属性的文件:

  1. Path file = ...;
  2. try {
  3. // Create the empty file with default permissions, etc.
  4. Files.createFile(file);
  5. } catch (FileAlreadyExistsException x) {
  6. System.err.format("file named %s" +
  7. " already exists%n", file);
  8. } catch (IOException x) {
  9. // Some other sort of failure, such as permissions.
  10. System.err.format("createFile error: %s%n", x);
  11. }

POSIX 文件权限有一个使用createFile(Path, FileAttribute&lt;?&gt;)创建具有预设权限的文件的示例。

您也可以使用newOutputStream方法创建新文件,如使用流 I / O 创建和写入文件中所述。如果打开新输出流并立即关闭它,则会创建一个空文件。

您可以使用以下createTempFile方法之一创建临时文件:

第一种方法允许代码指定临时文件的目录,第二种方法在默认临时文件目录中创建新文件。这两种方法都允许您为文件名指定后缀,第一种方法允许您指定前缀。以下代码段给出了第二种方法的示例:

  1. try {
  2. Path tempFile = Files.createTempFile(null, ".myapp");
  3. System.out.format("The temporary file" +
  4. " has been created: %s%n", tempFile)
  5. ;
  6. } catch (IOException x) {
  7. System.err.format("IOException: %s%n", x);
  8. }

运行此文件的结果如下所示:

  1. The temporary file has been created: /tmp/509668702974537184.myapp

临时文件名的特定格式是特定于平台的。