Reading and Writing Character Data Using Path Objects – Java I/O: Part II

Reading and Writing Character Data Using Path Objects

The Files class provides methods that directly use a Path object, and read and write characters to the file denoted by the Path object, without the need to specify an I/O stream. These methods also close the file when done.

Click here to view code image

static Path write(Path path, Iterable<? extends CharSequence> lines,
                  OpenOption… options) throws IOException
static Path write(Path path, Iterable<? extends CharSequence> lines,
                  Charset cs, OpenOption… options) throws IOException

Open or create a file denoted by the specified path, and writes text lines to the file, using either the UTF-8 charset or the specified charset, respectively.

No options implies the following options: CREATE, TRUNCATE_EXISTING, and WRITE.

Click here to view code image

static Path writeString(Path path, CharSequence csq, OpenOption… options)
            throws IOException
static Path writeString(Path path, CharSequence csq, Charset cs,
                        OpenOption… options) throws IOException

Open or create a file denoted by the specified path, and writes characters in the CharSequence csq verbatim to the file, using either the UTF-8 charset or the specified charset, respectively.

No options implies the following options: CREATE, TRUNCATE_EXISTING, and WRITE.

Click here to view code image

static String readString(Path path) throws IOException
static String readString(Path path, Charset cs) throws IOException

Read all content from a file denoted by the specified path into a string, decoding the bytes to characters using the UTF-8 charset or the specified charset, respectively. The string returned will contain all characters, including line separators. These methods are not recommended for reading large files.

Click here to view code image

static List<String> readAllLines(Path path) throws IOException
static List<String> readAllLines(Path path, Charset cs) throws IOException

Read all lines from the file denoted by the specified path, decoding the bytes to characters using the UTF-8 charset or the specified charset, respectively. These methods are not recommended for reading large files, as these can result in a lethal java.lang.OutOfMemoryError.

The code at (5) to (8) in Example 21.2 illustrates methods of the Files class that directly write and read character data to a file denoted by a Path object.

The write() method at (5) writes an Iterable (in this case, the List of String) to the file in one operation. It automatically terminates each string written with a newline.

Click here to view code image

Files.write(path, lines);                                       // (5)

The writeString() method at (6) writes the contents of a single CharSequence (in this case, the string joinedLines) to the file. The strings in the lines list are joined with an appropriate line separator by the String.join() method. The end result written to the file is again lines of text.

Click here to view code image

String joinedLines = String.join(System.lineSeparator(), lines);
Files.writeString(path, joinedLines);                            // (6)

The readString() method at (7) reads the whole file in one operation. It returns all characters read in a string, including any line separators.

Click here to view code image

String allContent = Files.readString(path);                      // (7)

The readAllLines() method at (8) reads all text lines in the file in one operation, returning the lines read in a List of String.

Click here to view code image

lines = Files.readAllLines(path);                                // (8)

The methods in the Files class for writing and reading directly from a file denoted by a Path object should be used with care, as they might not scale up when handling large files. This is especially the case regarding the readString() and readAll-Lines() methods that read the whole file in one fell swoop. A better solution for reading text files using streams is provided later in the chapter (p. 1345).

Categories: ,

Leave a Reply

Your email address will not be published. Required fields are marked *