Programming simple I/O operations is easy, which involves only a few classes and methods. You could do it by looking at a few samples. Programming efficient, portable I/O is extremely difficult, especially if you have to deal with different character sets. This explains why there are so many I/O packages [nine in JDK 1.7]!
JDK has two sets of I/O packages:
- the Standard I/O [in package
public void abstract void write[int unsignedByte] throws IOException
5], introduced since JDK 1.0 for stream-based I/O, and - the New I/O [in packages
public void abstract void write[int unsignedByte] throws IOException
6], introduced in JDK 1.4, for more efficient buffer-based I/O.
JDK 1.5 introduces the formatted text-I/O via new classes
public void abstract void write[int unsignedByte] throws IOException7 and
public void abstract void write[int unsignedByte] throws IOException8, and C-like
public void abstract void write[int unsignedByte] throws IOException9 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec0 methods for formatted output using format specifiers.
JDK 1.7 enhances supports for file I/O via the so-called NIO.2 [non-blocking I/O] in new package
File size is 417455 bytes Elapsed Time is 3781.500581 msec1 and its auxiliary packages. It also introduces a new
File size is 417455 bytes Elapsed Time is 3781.500581 msec2-with-resources syntax to simplify the coding of
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 method.
File and Directory
Class File size is 417455 bytes
Elapsed Time is 3781.500581 msec
4 [Pre-JDK 7]
The class
File size is 417455 bytes Elapsed Time is 3781.500581 msec4 can represent either a file or a directory. [JDK 1.7 introduces a more versatile
File size is 417455 bytes Elapsed Time is 3781.500581 msec6, which overcomes many limitations of
File size is 417455 bytes Elapsed Time is 3781.500581 msec4.]
A path string is used to locate a file or a directory. Unfortunately, path strings are system dependent, e.g., "
File size is 417455 bytes Elapsed Time is 3781.500581 msec8" in Windows or "
File size is 417455 bytes Elapsed Time is 3781.500581 msec9" in Unix/Mac.
- Windows use back-slash
File size is 417455 bytes Elapsed Time is 2.938921 msec
0 as the directory separator; while Unixes/Mac use forward-slashFile size is 417455 bytes Elapsed Time is 2.938921 msec
1. - Windows use semi-colon
File size is 417455 bytes Elapsed Time is 2.938921 msec
2 as path separator to separate a list of paths; while Unixes/Mac use colonFile size is 417455 bytes Elapsed Time is 2.938921 msec
3. - Windows use "
File size is 417455 bytes Elapsed Time is 2.938921 msec
4" as line delimiter for text file; while Unixes use "File size is 417455 bytes Elapsed Time is 2.938921 msec
5" and Mac uses "File size is 417455 bytes Elapsed Time is 2.938921 msec
6". - The "
File size is 417455 bytes Elapsed Time is 2.938921 msec
7" or "File size is 417455 bytes Elapsed Time is 2.938921 msec
8" is called the root. Windows supports multiple roots, each maps to a drive [e.g., "File size is 417455 bytes Elapsed Time is 2.938921 msec
7", "File size is 417455 bytes Elapsed Time is 61.834954 msec
0"]. Unixes/Mac has a single root ["File size is 417455 bytes Elapsed Time is 2.938921 msec
8"].
A path could be absolute [beginning from the root] or relative [which is relative to a reference directory]. Special notations "
File size is 417455 bytes Elapsed Time is 61.834954 msec2" and "
File size is 417455 bytes Elapsed Time is 61.834954 msec3" denote the current directory and the parent directory, respectively.
The
File size is 417455 bytes Elapsed Time is 3781.500581 msec4 class maintains these system-dependent properties, for you to write programs that are portable:
- Directory Separator: in
File size is 417455 bytes Elapsed Time is 61.834954 msec
5 fieldsFile size is 417455 bytes Elapsed Time is 61.834954 msec
6 [asFile size is 417455 bytes Elapsed Time is 61.834954 msec
7] andFile size is 417455 bytes Elapsed Time is 61.834954 msec
8. [They failed to follow the Java naming convention for constants adopted since JDK 1.2.] As mentioned, Windows use backslashFile size is 417455 bytes Elapsed Time is 2.938921 msec
0; while Unixes/Mac use forward slashFile size is 417455 bytes Elapsed Time is 2.938921 msec
1. - Path Separator: in
File size is 417455 bytes Elapsed Time is 61.834954 msec
5 fieldsDataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];
2 [asFile size is 417455 bytes Elapsed Time is 61.834954 msec
7] andDataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];
4. As mentioned, Windows use semi-colonFile size is 417455 bytes Elapsed Time is 2.938921 msec
2 to separate a list of paths; while Unixes/Mac use colonFile size is 417455 bytes Elapsed Time is 2.938921 msec
3.
You can construct a
DataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];7 instance with a path string or URI, as follows. Take note that the physical file/directory may or may not exist. A file URL takes the form of
DataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];8, e.g.,
DataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];9.
For examples,
For applications that you intend to distribute as JAR files, you should use the
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];0 class to reference the resources, as it can reference disk files as well as JAR'ed files , for example,
java.net.URL url = this.getClass[].getResource["icon.png"];Verifying Properties of a File/DirectoryList Directory
For a directory, you can use the following methods to list its contents:
Example: The following program recursively lists the contents of a given directory [similar to Unix's
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];1 command].[Advanced] List Directory with Filter
You can apply a filter to
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];2 and
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];3, to list only files that meet a certain criteria.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]
The
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];4
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];5 declares one
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 method:
public boolean accept[File dirName, String fileName]
The
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];2 and
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];3 methods does a call-back to
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];9 for each of the file/sub-directory produced. You can program your filtering criteria in
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];9. Those files/sub-directories that result in a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]01 return will be excluded.
Example: The following program lists only files that meet a certain filtering criteria.
Class File size is 417455 bytes
Elapsed Time is 3781.500581 msec
6 [JDK 7]
Read "".
Stream I/O in Standard I/O [java.io Package]
Programs read inputs from data sources [e.g., keyboard, file, network, memory buffer, or another program] and write outputs to data sinks [e.g., display console, file, network, memory buffer, or another program]. In Java standard I/O, inputs and outputs are handled by the so-called streams. A stream is a sequential and contiguous one-way flow of data [just like water or oil flows through the pipe]. It is important to mention that Java does not differentiate between the various types of data sources or sinks [e.g., file or network] in stream I/O. They are all treated as a sequential flow of data. Input and output streams can be established from/to any data source/sink, such as files, network, keyboard/console or another program. The Java program receives data from a source by opening an input stream, and sends data to a sink by opening an output stream. All Java I/O streams are one-way [except the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03, which will be discussed later]. If your program needs to perform both input and output, you have to open two streams - an input stream and an output stream.
Stream I/O operations involve three steps:
- Open an input/output stream associated with a physical device [e.g., file, network, console/keyboard], by constructing an appropriate I/O stream instance.
- Read from the opened input stream until "end-of-stream" encountered, or write to the opened output stream [and optionally flush the buffered output].
- Close the input/output stream.
Java's I/O operations is more complicated than C/C++ to support internationalization [i18n]. Java internally stores characters [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 type] in 16-bit UCS-2 character set. But the external data source/sink could store characters in other character set [e.g., US-ASCII, ISO-8859-x, UTF-8, UTF-16, and many others], in fixed length of 8-bit or 16-bit, or in variable length of 1 to 4 bytes. [Read ""]. As a consequence, Java needs to differentiate between byte-based I/O for processing raw bytes or binary data, and character-based I/O for processing texts made up of characters.
Byte-Based I/O & Byte Streams
Byte streams are used to read/write raw bytes serially from/to an external device. All the byte streams are derived from the
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclasses
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07, as illustrated in the class diagram.
Reading from an InputStream
The
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclass
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 declares an
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 method
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 to read one data-byte from the input source:
public abstract int read[] throws IOException
The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 method:
- returns the input byte read as an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]
13 in the range of 0 to 255, or - returns -1 if "end of stream" condition is detected, or
- throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]
14 if it encounters an I/O error.
The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 method returns an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13 instead of a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]17, because it uses -1 to indicate end-of-stream.
The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 method blocks until a byte is available, an I/O error occurs, or the "end-of-stream" is detected. The term "block" means that the method [and the program] will be suspended. The program will resume only when the method returns.
Two variations of
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 methods are implemented in the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 for reading a block of bytes into a byte-array. It returns the number of bytes read, or -1 if "end-of-stream" encounters.
Writing to an OutputStream
Similar to the input counterpart, the
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclass
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 declares an abstract method
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]23 to write a data-byte to the output sink.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]23 takes an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13. The least-significant byte of the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13 argument is written out; the upper 3 bytes are discarded. It throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14 if I/O error occurs [e.g., output stream has been closed].
public void abstract void write[int unsignedByte] throws IOException
Similar to the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11, two variations of the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]23 method to write a block of bytes from a byte-array are implemented:
Opening & Closing I/O Streams
You open an I/O stream by constructing an instance of the stream. Both the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 provides a
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 method to close the stream, which performs the necessary clean-up operations to free up the system resources.
It is a good practice to explicitly close the I/O stream, by running
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 in the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]34 clause of
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]35 to free up the system resources immediately when the stream is no longer needed. This could prevent serious resource leaks. Unfortunately, the
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 method also throws a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14, and needs to be enclosed in a nested
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]38 statement, as follows. This makes the codes somehow ugly.
JDK 1.7 introduces a new
File size is 417455 bytes Elapsed Time is 3781.500581 msec2-with-resources syntax, which automatically closes all the opened resources after
File size is 417455 bytes Elapsed Time is 3781.500581 msec2 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]41, as follows. This produces much neater codes.
Flushing the OutputStream
In addition, the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 provides a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]43 method to flush the remaining bytes from the output buffer.
Implementations of abstract InputStream/OutputStream
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 are
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 classes that cannot be instantiated. You need to choose an appropriate concrete subclass to establish a connection to a physical device. For example, you can instantiate a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 to establish a stream to a physical disk file.
Layered [or Chained] I/O Streams
The I/O streams are often layered or chained with other I/O streams, for purposes such as buffering, filtering, or data-format conversion [between raw bytes and primitive types]. For example, we can layer a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]49 to a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 for buffered input, and stack a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 in front for formatted data input [using primitives such as
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]53], as illustrated in the following diagrams.
File I/O Byte-Streams - FileInputStream & FileOutputStream
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 are concrete implementations to the
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 classes
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07, to support I/O from disk files.
Buffered I/O Byte-Streams - BufferedInputStream & BufferedOutputStream
The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11/
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]23 method in
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06/
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 are designed to read/write a single byte of data on each call. This is grossly inefficient, as each call is handled by the underlying operating system [which may trigger a disk access, or other expensive operations]. Buffering, which reads/writes a block of bytes from the external device into/from a memory buffer in a single I/O operation, is commonly applied to speed up the I/O.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47/
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 is not buffered. It is often chained to a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]49 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]66, which provides the buffering. To chain the streams together, simply pass an instance of one stream into the constructor of another stream. For example, the following codes chain a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 to a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]49, and finally, a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51:
Example 1: Copying a file byte-by-byte without Buffering.
File size is 417455 bytes Elapsed Time is 3781.500581 msec
This example copies a file by reading a byte from the input file and writing it to the output file. It uses
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 directly without buffering. Notice that most the I/O methods "throws"
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14, which must be caught or declared to be thrown. The method
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 is programmed inside the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]34 clause. It is guaranteed to be run after
File size is 417455 bytes Elapsed Time is 3781.500581 msec2 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]41. However, method
File size is 417455 bytes Elapsed Time is 3781.500581 msec3 also throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14, and therefore must be enclosed inside a nested
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]38 block, which makes the codes a little ugly.
I used
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]80, which was introduced in JDK 1.5, for a more accurate measure of the elapsed time, instead of the legacy not-so-precise
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]81. The output shows that it took about 4 seconds to copy a 400KB file.
As mentioned, JDK 1.7 introduces a new
File size is 417455 bytes Elapsed Time is 3781.500581 msec2-with-resources syntax, which automatically closes all the resources opened, after
File size is 417455 bytes Elapsed Time is 3781.500581 msec2 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]41. For example, the above example can be re-written in a much neater manner as follow:
Example 2: Copying a file with a Programmer-Managed Buffer.
File size is 417455 bytes Elapsed Time is 2.938921 msec
This example again uses
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 directly. However, instead of reading/writing one byte at a time, it reads/writes a 4KB block. This program took only 3 millisecond - a more than 1000 times speed-up compared with the previous example.
Larger buffer size, up to a certain limit, generally improves the I/O performance. However, there is a trade-off between speed-up the the memory usage. For file copying, a large buffer is certainly recommended. But for reading just a few bytes from a file, large buffer simply wastes the memory.
I re-write the program using JDK 1.7, and try on various buffer size on a much bigger file of 26MB.
Increasing buffer size helps only up to a certain point?!
Example 3: Copying a file with Buffered Streams.
File size is 417455 bytes Elapsed Time is 61.834954 msec
In this example, I chain the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 with
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]49,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48 with
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]66, and read/write byte-by-byte. The JRE decides on the buffer size. The program took 62 milliseconds, about 60 times speed-up compared with example 1, but slower than the programmer-managed buffer.
The JDK 1.7 version of the above example is as follows:
Formatted Data-Streams: DataInputStream & DataOutputStream
The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]92 can be stacked on top of any
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 to parse the raw bytes so as to perform I/O operations in the desired data format, such as
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]53.
To use
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 for formatted input, you can chain up the input streams as follows:
DataInputStream in = new DataInputStream[ new BufferedInputStream[ new FileInputStream["in.dat"]]];
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 implements
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]99 interface, which provides methods to read formatted primitive data and
File size is 417455 bytes Elapsed Time is 61.834954 msec7, such as:
Similarly, you can stack the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]92 as follows:
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]92 implements
public boolean accept[File dirName, String fileName]03 interface, which provides methods to write formatted primitive data and
File size is 417455 bytes Elapsed Time is 61.834954 msec7. For examples,
Example: The following program writes some primitives to a disk file. It then reads the raw bytes to check how the primitives were stored. Finally, it reads the data as primitives.
The data stored in the disk are exactly in the same form as in the Java program internally [e.g., UCS-2 for characters]. The byte-order is big-endian [big byte first, or most significant byte in lowest address].
Network I/O
[In Java Networking]
Character-Based I/O & Character Streams
Java internally stores characters [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 type] in 16-bit UCS-2 character set. But the external data source/sink could store characters in other character set [e.g., US-ASCII, ISO-8859-x, UTF-8, UTF-16, and many others], in fixed length of 8-bit or 16-bit, or in variable length of 1 to 4 bytes. [Read ""]. Hence, Java has to differentiate between byte-based I/O for processing 8-bit raw bytes, and character-based I/O for processing texts. The character streams needs to translate between the character set used by external I/O devices and Java internal UCS-2 format. For example, the character '您' is stored as "
public boolean accept[File dirName, String fileName]06" in UCS-2 [Java internal], "
public boolean accept[File dirName, String fileName]07" in UTF8, "
public boolean accept[File dirName, String fileName]08" in GBK/GB2312, and "
public boolean accept[File dirName, String fileName]09" in BIG5. If this character is to be written to a file uses UTF-8, the character stream needs to translate "
public boolean accept[File dirName, String fileName]06" to "
public boolean accept[File dirName, String fileName]07". The reserve takes place in a reading operation.
The byte/character streams refer to the unit of operation within the Java programs, which does not necessary correspond to the amount of data transferred from/to the external I/O devices. This is because some charsets use fixed-length of 8-bit [e.g., US-ASCII, ISO-8859-1] or 16-bit [e.g., UCS-16], while some use variable-length of 1-4 bytes [e.g., UTF-8, UTF-16, UTF-16-BE, UTF-16-LE, GBK, BIG5]. When a character stream is used to read an 8-bit ASCII file, an 8-bit data is read from the file and put into the 16-bit
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 location of the Java program.
Abstract superclass Reader and Writer
Other than the unit of operation and charset conversion [which is extremely complex], character-based I/O is almost identical to byte-based I/O. Instead of
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07, we use
public boolean accept[File dirName, String fileName]15 and
public boolean accept[File dirName, String fileName]16 for character-based I/O.
The
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclass
public boolean accept[File dirName, String fileName]15 operates on
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04. It declares an
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 method
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 to read one character from the input source.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 returns the character as an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13 between 0 to 65535 [a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 in Java can be treated as an unsigned 16-bit integer]; or -1 if end-of-stream is detected; or throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14 if I/O error occurs. There are also two variations of
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 to read a block of characters into
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04-array.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]0
The
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclass
public boolean accept[File dirName, String fileName]16 declares an
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 method
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]23, which writes a character to the output sink. The lower 2 bytes of the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13 argument is written out; while the upper 2 bytes are discarded.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]1
File I/O Character-Streams - FileReader & FileWriter
public boolean accept[File dirName, String fileName]33 and
public boolean accept[File dirName, String fileName]34 are concrete implementations to the
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 superclasses
public boolean accept[File dirName, String fileName]15 and
public boolean accept[File dirName, String fileName]16, to support I/O from disk files.
public boolean accept[File dirName, String fileName]33/
public boolean accept[File dirName, String fileName]34 assumes that the default character encoding [charset] is used for the disk file. The default charset is kept in the JVM's system property "
public boolean accept[File dirName, String fileName]40". You can get the default charset via
File size is 417455 bytes Elapsed Time is 61.834954 msec5 method
public boolean accept[File dirName, String fileName]42 or
public boolean accept[File dirName, String fileName]43. It is probable safe to use
public boolean accept[File dirName, String fileName]33/
public boolean accept[File dirName, String fileName]34 for ASCII texts, provided that the default charset is compatible to ASCII [such as US-ASCII, ISO-8859-x, UTF-8, and many others, but NOT UTF-16, UTF-16BE, UTF-16LE and many others]. Use of
public boolean accept[File dirName, String fileName]46 is NOT recommended as you have no control of the file encoding charset.
Buffered I/O Character-Streams - BufferedReader & BufferedWriter
public boolean accept[File dirName, String fileName]47 and
public boolean accept[File dirName, String fileName]48 can be stacked on top of
public boolean accept[File dirName, String fileName]46 or other character streams to perform buffered I/O, instead of character-by-character.
public boolean accept[File dirName, String fileName]47 provides a new method
public boolean accept[File dirName, String fileName]51, which reads a line and returns a
File size is 417455 bytes Elapsed Time is 61.834954 msec7 [without the line delimiter]. Lines could be delimited by "
File size is 417455 bytes Elapsed Time is 2.938921 msec5" [Unix], "
File size is 417455 bytes Elapsed Time is 2.938921 msec4" [Windows], or "
File size is 417455 bytes Elapsed Time is 2.938921 msec6" [Mac].Example
Character Set [or Charset] - Package java.nio.charset [JDK 1.4]
JDK 1.4 provides a new package
public boolean accept[File dirName, String fileName]56 as part of NIO [New IO] to support character translation between the Unicode [UCS-2] used internally in Java program and external devices which could be encoded in any other format [e.g., US-ASCII, ISO-8859-x, UTF-8, UTF-16, UTF-16BE, UTF-16LE, and etc.]
The main class
public boolean accept[File dirName, String fileName]57 provides
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods for testing whether a particular charset is supported, locating charset instances by name, and listing all the available charsets and the default charset.Example
The default charset for file encoding is kept in the system property "
public boolean accept[File dirName, String fileName]40". To change the JVM's default charset for file encoding, you can use command-line VM option "
public boolean accept[File dirName, String fileName]60". For example, the following command run the program with default charset of UTF-8.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]2
Most importantly, the
public boolean accept[File dirName, String fileName]61 class provides methods to encode/decode characters from UCS-2 used in Java program and the specific charset used in the external devices [such as UTF-8].
The
public boolean accept[File dirName, String fileName]62/
public boolean accept[File dirName, String fileName]63 methods operate on
public boolean accept[File dirName, String fileName]64 and
public boolean accept[File dirName, String fileName]65 introduced also in JDK 1.4, which will be explain in the New I/O section.
Example: The following example encodes some Unicode texts in various encoding scheme, and display the Hex codes of the encoded byte sequences.
Example: The following example tries out the encoding/decoding on
public boolean accept[File dirName, String fileName]65 and
public boolean accept[File dirName, String fileName]64. Buffers will be discussed later under New I/O.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]3
Text File I/O - InputStreamReader and OutputStreamWriter
As mentioned, Java internally stores characters [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 type] in 16-bit UCS-2 character set. But the external data source/sink could store characters in other character set [e.g., US-ASCII, ISO-8859-x, UTF-8, UTF-16, and many others], in fixed length of 8-bit or 16-bit, or in variable length of 1 to 4 bytes. The
public boolean accept[File dirName, String fileName]33/
public boolean accept[File dirName, String fileName]34 introduced earlier uses the default charset for decoding/encoding, resulted in non-portable programs.
To choose the charset, you need to use
public boolean accept[File dirName, String fileName]71 and
public boolean accept[File dirName, String fileName]72.
public boolean accept[File dirName, String fileName]71 and
public boolean accept[File dirName, String fileName]72 are considered to be byte-to-character "bridge" streams.
You can choose the character set in the
public boolean accept[File dirName, String fileName]71's constructor:
You can list the available charsets via
File size is 417455 bytes Elapsed Time is 61.834954 msec5 method
public boolean accept[File dirName, String fileName]77. The commonly-used
public boolean accept[File dirName, String fileName]61 names supported by Java are:
- "
public boolean accept[File dirName, String fileName]
79": 7-bit ASCII [aka ISO646-US] - "
public boolean accept[File dirName, String fileName]
80": Latin-1 - "
public boolean accept[File dirName, String fileName]
81": Most commonly-used encoding scheme for Unicode - "
public boolean accept[File dirName, String fileName]
82": Big-endian [big byte first] [big-endian is usually the default] - "
public boolean accept[File dirName, String fileName]
83": Little-endian [little byte first] - "
public boolean accept[File dirName, String fileName]
84": with a 2-byte BOM [Byte-Order-Mark] to specify the byte order. FE FF indicates big-endian, FF FE indicates little-endian.
As the
public boolean accept[File dirName, String fileName]71/
public boolean accept[File dirName, String fileName]72 often needs to read/write in multiple bytes, it is best to wrap it with a
public boolean accept[File dirName, String fileName]47/
public boolean accept[File dirName, String fileName]48.
Example: The following program writes Unicode texts to a disk file using various charsets for file encoding. It then reads the file byte-by-byte [via a byte-based input stream] to check the encoded characters in the various charsets. Finally, it reads the file using the character-based reader.
As seen from the output, the characters 您好 is encoded differently in different charsets. Nonetheless, the
public boolean accept[File dirName, String fileName]71 is able to translate the characters into the same UCS-2 used in Java program.
java.io.PrintStream & java.io.PrintWriter
The byte-based
public boolean accept[File dirName, String fileName]90 supports convenient printing methods such as
public boolean accept[File dirName, String fileName]91 and
public boolean accept[File dirName, String fileName]92 for printing primitives and text string. Primitives are converted to their string representation for printing. The
public void abstract void write[int unsignedByte] throws IOException9 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec0 were introduced in JDK 1.5 for formatting output with former specifiers.
public void abstract void write[int unsignedByte] throws IOException9 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec0 are identical.
A
public boolean accept[File dirName, String fileName]97 never throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14. Instead, it sets an internal flag which can be checked via the
public boolean accept[File dirName, String fileName]99 method. A
public boolean accept[File dirName, String fileName]97 can also be created to flush the output automatically. That is, the
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]43 method is automatically invoked after a byte array is written, one of the
public abstract int read[] throws IOException02] methods is invoked, or after a newline [
public abstract int read[] throws IOException03] is written.
The standard output and error streams [
public abstract int read[] throws IOException04 and
public abstract int read[] throws IOException05] belong to
public boolean accept[File dirName, String fileName]97.
All characters printed by a
public boolean accept[File dirName, String fileName]97 are converted into bytes using the default character encoding. The
public abstract int read[] throws IOException08 class should be used in situations that require writing characters rather than bytes.
The character-stream
public abstract int read[] throws IOException08 is similar to
public boolean accept[File dirName, String fileName]97, except that it write in characters instead of bytes. The
public abstract int read[] throws IOException08 also supports all the convenient printing methods
public boolean accept[File dirName, String fileName]91,
public boolean accept[File dirName, String fileName]92,
public void abstract void write[int unsignedByte] throws IOException9 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec0. It never throws an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]14 and can optionally be created to support automatic flushing.
[TODO] Example to show the difference between
public boolean accept[File dirName, String fileName]97 and
public abstract int read[] throws IOException08.
Object Serialization and Object Streams
Data streams [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]92] allow you to read and write primitive data [such as
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]53] and
File size is 417455 bytes Elapsed Time is 61.834954 msec7, rather than individual bytes. Object streams [
public abstract int read[] throws IOException24 and
public abstract int read[] throws IOException25] go one step further to allow you to read and write entire objects [such as
public abstract int read[] throws IOException26,
public abstract int read[] throws IOException27 or any custom objects].
Object serialization is the process of representing a "particular state of an object" in a serialized bit-stream, so that the bit stream can be written out to an external device [such as a disk file or network]. The bit-stream can later be re-constructed to recover the state of that object. Object serialization is necessary to save a state of an object into a disk file for persistence or sent the object across the network for applications such as Web Services, Distributed-object applications, and Remote Method Invocation [RMI].
In Java, object that requires to be serialized must implement
public abstract int read[] throws IOException28 or
public abstract int read[] throws IOException29 interface.
public abstract int read[] throws IOException30 interface is an empty interface [or tagged interface] with nothing declared. Its purpose is simply to declare that particular object is serializable.
ObjectInputStream & ObjectOutputStream
The
public abstract int read[] throws IOException24 and
public abstract int read[] throws IOException25 can be used to serialize an object into a bit-stream and transfer it to/from an I/O streams, via these methods:
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]4
public abstract int read[] throws IOException24 and
public abstract int read[] throws IOException25 must be stacked on top of a concrete implementation of
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07, such as
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]47 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]48.
For example, the following code segment writes objects to a disk file. The "
public abstract int read[] throws IOException39" is the convention for serialized object file type.
To read and re-construct the object back in a program, use the method
public abstract int read[] throws IOException40, which returns an
public abstract int read[] throws IOException41. Downcast the
public abstract int read[] throws IOException42 back to its original type.
Example: Object serialization
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]5
[Check out these bytes!]
Primitive types and array are, by default, serializable.
The
public abstract int read[] throws IOException24 and
public abstract int read[] throws IOException25 implement
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]99 and
public boolean accept[File dirName, String fileName]03 interface respectively. You can used methods such as
public abstract int read[] throws IOException47,
public abstract int read[] throws IOException48,
public abstract int read[] throws IOException49,
public abstract int read[] throws IOException50 for reading and writing primitive types.transient & static
File size is 417455 bytes Elapsed Time is 61.834954 msec
5 fields are not serialized, as it belongs to the class instead of the particular instance to be serialized.- To prevent certain fields from being serialized, mark them using the keyword
public abstract int read[] throws IOException
52. This could cut down the amount of data traffic. - The
public abstract int read[] throws IOException
53 method writes out the class of the object, the class signature, and values of non-File size is 417455 bytes Elapsed Time is 61.834954 msec
5 and non-public abstract int read[] throws IOException
52 fields.
java.io.Serializable & Externalizable Interfaces
When you create a class that might be serialized, the class must implement
public abstract int read[] throws IOException28 interface. The
public abstract int read[] throws IOException30 interface doesn't declare any methods. Empty interfaces such as
public abstract int read[] throws IOException30 are known as tagging interfaces. They identify implementing classes as having certain properties, without requiring those classes to actually implement any methods.
Most of the core Java classes implement
public abstract int read[] throws IOException30, such as all the wrapper classes, collection classes, and GUI classes. In fact, the only core Java classes that do not implement
public abstract int read[] throws IOException30 are ones that should not be serialized. Arrays of primitives or serializable objects are themselves serializable.
This warning message is triggered because your class [such as
public abstract int read[] throws IOException61] implements the
public abstract int read[] throws IOException28 interface. This interface enables the object to be written out to an output stream serially [via method
public abstract int read[] throws IOException53]; and read back into the program [via method
public abstract int read[] throws IOException40]. The serialization runtime uses a number [called
public abstract int read[] throws IOException65] to ensure that the object read into the program [during deserialization] is compatible with the class definition, and not belonging to another version. It throws an
public abstract int read[] throws IOException66 otherwise.
You have these options:
- Simply ignore this warning message. If a
public abstract int read[] throws IOException
67 class does not explicitly declare apublic abstract int read[] throws IOException
65, then the serialization runtime will calculate a defaultpublic abstract int read[] throws IOException
65 value for that class based on various aspects of the class. - Add a
public abstract int read[] throws IOException
65 [Recommended], e.g. - Suppress this particular warning via annotation
public abstract int read[] throws IOException
71 [in packagepublic abstract int read[] throws IOException
72] [JDK 1.5]:public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]
6
The
public abstract int read[] throws IOException30 has a sub-interface called
public abstract int read[] throws IOException74, which you could used if you want to customize the way a class is serialized. Since
public abstract int read[] throws IOException74 extends
public abstract int read[] throws IOException30, it is also a
public abstract int read[] throws IOException30 and you could invoke
public abstract int read[] throws IOException40 and
public abstract int read[] throws IOException53.
public abstract int read[] throws IOException74 declares two
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 methods:
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]7
public abstract int read[] throws IOException82 and
public abstract int read[] throws IOException83 are interfaces that are implemented by
public abstract int read[] throws IOException25 and
public abstract int read[] throws IOException24, which define the
public abstract int read[] throws IOException53 and
public abstract int read[] throws IOException40 methods, respectively. When an instance of
public abstract int read[] throws IOException74 is passed to an
public abstract int read[] throws IOException25, the default serialization procedure is bypassed; instead, the stream calls the instance's
public abstract int read[] throws IOException90 method. Similarly, when an
public abstract int read[] throws IOException24 reads a
public abstract int read[] throws IOException92 instance, it uses
public abstract int read[] throws IOException93 to reconstruct the instance.
public abstract int read[] throws IOException74 is useful if you want complete control on how your objects shall be serialized/deserialized. For example, you could encrypt sensitive data before the object is serialized.
Random Access Files
All the I/O streams covered so far are one-way streams. That is, they are either read-only input stream or write-only output stream. Furthermore, they are all sequential-access [or serial] streams, meant for reading and writing data sequentially. Nonetheless, it is sometimes necessary to read a file record directly as well as modifying existing records or inserting new records. The class
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03 provides supports for non-sequential, direct [or random] access to a disk file.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03 is a two-way stream, supporting both input and output operations in the same stream.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03 can be treated as a huge byte array. You can use a file pointer [of type
public abstract int read[] throws IOException98], similar to array index, to access individual byte or group of bytes in primitive types [such as int and double]. The file pointer is located at 0 when the file is opened. It advances automatically for every read and write operation by the number of bytes processed.
In constructing a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03, you can use flags
public void abstract void write[int unsignedByte] throws IOException00 or
public void abstract void write[int unsignedByte] throws IOException01 to indicate whether the file is "read-only" or "read-write" access, e.g.,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]8
The following methods are available:
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03 does not inherit from
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 or
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07. However, it implements
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]99 and
public boolean accept[File dirName, String fileName]03 interfaces [similar to
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]51 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]92]. Therefore, you can use various methods to read/write primitive types to the file, e.g.,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]9
Example: Read and write records from a
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]03. [A student file consists of student record of name [
File size is 417455 bytes Elapsed Time is 61.834954 msec7] and id [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13]].
[PENDING]
Compressed I/O Streams
The classes
public void abstract void write[int unsignedByte] throws IOException12 and
public void abstract void write[int unsignedByte] throws IOException13 [in package
public void abstract void write[int unsignedByte] throws IOException14] support reading and writing of compressed data in ZIP format. The classes
public void abstract void write[int unsignedByte] throws IOException15 and
public void abstract void write[int unsignedByte] throws IOException16 [in package
public void abstract void write[int unsignedByte] throws IOException14] support reading and writing of compressed data in GZIP format.
Example: Reading and writing ZIP files
[@PENDING]
Example: Reading and writing JAR files
[@PENDING]
Formatted Text I/O - java.util.Scanner & java.util.Formatter [JDK 1.5]
Formatted-Text Input via java.util.Scanner
JDK 1.5 introduces
public void abstract void write[int unsignedByte] throws IOException7 class, which greatly simplifies formatted text input from input source [e.g., files, keyboard, network].
public void abstract void write[int unsignedByte] throws IOException19, as the name implied, is a simple text scanner which can parse the input text into primitive types and strings using regular expressions. It first breaks the text input into tokens using a delimiter pattern, which is by default the white spaces [blank, tab and newline]. The tokens may then be converted into primitive values of different types using the various
public void abstract void write[int unsignedByte] throws IOException20 methods [
public void abstract void write[int unsignedByte] throws IOException21,
public void abstract void write[int unsignedByte] throws IOException22,
public void abstract void write[int unsignedByte] throws IOException23,
public void abstract void write[int unsignedByte] throws IOException24,
public void abstract void write[int unsignedByte] throws IOException25,
public void abstract void write[int unsignedByte] throws IOException26,
public void abstract void write[int unsignedByte] throws IOException27,
public void abstract void write[int unsignedByte] throws IOException28 for
File size is 417455 bytes Elapsed Time is 61.834954 msec7, and
public void abstract void write[int unsignedByte] throws IOException30 for an input line]. You can also use the
public void abstract void write[int unsignedByte] throws IOException31 methods to check for the availability of a desired input.
The commonly-used constructors are as follows. You can construct a
public void abstract void write[int unsignedByte] throws IOException19 to parse a byte-based
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 [e.g.,
public void abstract void write[int unsignedByte] throws IOException34], a disk file, or a given
File size is 417455 bytes Elapsed Time is 61.834954 msec7.
For examples,
Example 1: The most common usage of
public void abstract void write[int unsignedByte] throws IOException19 is to read primitive types and
File size is 417455 bytes Elapsed Time is 61.834954 msec7 form the keyboard [
public void abstract void write[int unsignedByte] throws IOException34], as follows:
The
public void abstract void write[int unsignedByte] throws IOException20 methods throw
public void abstract void write[int unsignedByte] throws IOException40 if the next token does not match the type to be parsed.
Example 2: You can easily modify the above program to read the inputs from a text file, instead of keyboard [
public void abstract void write[int unsignedByte] throws IOException34].
Prepare the input file "
public void abstract void write[int unsignedByte] throws IOException42" as follows:
public boolean accept[File dirName, String fileName]0nextXxx[] and hasNextXxx[]
The
public void abstract void write[int unsignedByte] throws IOException19 class implements
public void abstract void write[int unsignedByte] throws IOException44 interface. You can use
public void abstract void write[int unsignedByte] throws IOException45 coupled with
public void abstract void write[int unsignedByte] throws IOException28 to iterate through all the
File size is 417455 bytes Elapsed Time is 61.834954 msec7 tokens. You can also directly iterate through the primitive types via methods
public void abstract void write[int unsignedByte] throws IOException31 and
public void abstract void write[int unsignedByte] throws IOException20.
public void abstract void write[int unsignedByte] throws IOException50 includes all primitive types [
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]17,
public void abstract void write[int unsignedByte] throws IOException52,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]13,
public abstract int read[] throws IOException98,
public void abstract void write[int unsignedByte] throws IOException55,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]53 and
public void abstract void write[int unsignedByte] throws IOException57],
public void abstract void write[int unsignedByte] throws IOException58 and
public void abstract void write[int unsignedByte] throws IOException59.
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]04 is not included but can be retrieved from
File size is 417455 bytes Elapsed Time is 61.834954 msec7 via
public void abstract void write[int unsignedByte] throws IOException62.Delimiter
Instead of the default white spaces as the delimiter, you can set the delimiter to a chosen regular expression via these methods:
Example 3: Customized token delimiter
The regular expression
public void abstract void write[int unsignedByte] throws IOException63 matches zero or more white spaces [
public void abstract void write[int unsignedByte] throws IOException64] followed by "apple" followed by zero or more white spaces [
public void abstract void write[int unsignedByte] throws IOException64]. An additional backslash [
File size is 417455 bytes Elapsed Time is 2.938921 msec8] is needed to use a backslash [
File size is 417455 bytes Elapsed Time is 2.938921 msec8] in Java
File size is 417455 bytes Elapsed Time is 61.834954 msec7's literal. Read "Regular Expression" for more details.Regexe Pattern Matching
You can use the following methods to find the next occurrence of the specified pattern using regular expressions:
CharsetBy default,
public void abstract void write[int unsignedByte] throws IOException19 uses the default charset to read the character from the input source. You can ask
public void abstract void write[int unsignedByte] throws IOException19 to read text file which is encoded using a particular charset, by providing the charset name.
Example 4:
Formatted-Text Printing with printf[] method
JDK 1.5 introduces C-like
public void abstract void write[int unsignedByte] throws IOException9 method [in classes
public void abstract void write[int unsignedByte] throws IOException72 and
public void abstract void write[int unsignedByte] throws IOException73] for formatted-text printing.
To write formatted-text to console [
public abstract int read[] throws IOException04 or
public abstract int read[] throws IOException05], you could simply use the convenience methods
public void abstract void write[int unsignedByte] throws IOException76 or
public void abstract void write[int unsignedByte] throws IOException77.
public void abstract void write[int unsignedByte] throws IOException9 takes this syntax:
public boolean accept[File dirName, String fileName]1
public void abstract void write[int unsignedByte] throws IOException9 takes a variable number [zero or more] of arguments [or varargs]. Varargs was introduced in JDK 1.5 [that is the reason Java cannot support
public void abstract void write[int unsignedByte] throws IOException9 earlier].
public void abstract void write[int unsignedByte] throws IOException9 can be called from
public abstract int read[] throws IOException04 or
public abstract int read[] throws IOException05 [ which are
public boolean accept[File dirName, String fileName]97s]. For example,
public boolean accept[File dirName, String fileName]2
public boolean accept[File dirName, String fileName]3
You can also use the
public void abstract void write[int unsignedByte] throws IOException77 method, which is identical to
public void abstract void write[int unsignedByte] throws IOException76.Format Specifiers
A format specifier begins with
public void abstract void write[int unsignedByte] throws IOException87 and ends with a conversion-type character [e.g.
public void abstract void write[int unsignedByte] throws IOException88 for integer,
public void abstract void write[int unsignedByte] throws IOException89 for
public void abstract void write[int unsignedByte] throws IOException55 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]53], with optional parameters in between, as follows:
public boolean accept[File dirName, String fileName]4
- The optional argument_position specifies the position of the argument in the argument list. The first argument is
public void abstract void write[int unsignedByte] throws IOException
92, second argument ispublic void abstract void write[int unsignedByte] throws IOException
93, and so on. - The optional width indicates the minimum number of characters to be output.
- The optional precision restricts the number of characters [or number of decimal places for float-point numbers].
- The mandatory conversion-type-character indicates how the argument should be formatted. For examples:
public void abstract void write[int unsignedByte] throws IOException
94,public void abstract void write[int unsignedByte] throws IOException
95 [public void abstract void write[int unsignedByte] throws IOException
57],public void abstract void write[int unsignedByte] throws IOException
97,public void abstract void write[int unsignedByte] throws IOException
98 [hex string],public void abstract void write[int unsignedByte] throws IOException
99,File size is 417455 bytes Elapsed Time is 3781.500581 msec
00 [string],File size is 417455 bytes Elapsed Time is 3781.500581 msec
01,File size is 417455 bytes Elapsed Time is 3781.500581 msec
02 [character],File size is 417455 bytes Elapsed Time is 3781.500581 msec
03 [decimal integer],File size is 417455 bytes Elapsed Time is 3781.500581 msec
04 [octal integer],File size is 417455 bytes Elapsed Time is 3781.500581 msec
05,File size is 417455 bytes Elapsed Time is 3781.500581 msec
06 [hexadecimal integer],File size is 417455 bytes Elapsed Time is 3781.500581 msec
07,File size is 417455 bytes Elapsed Time is 3781.500581 msec
08 [float-point number in scientific notation],File size is 417455 bytes Elapsed Time is 3781.500581 msec
09 [floating-point number],public void abstract void write[int unsignedByte] throws IOException
87 [percent sign]. The uppercase conversion code [e.g.,File size is 417455 bytes Elapsed Time is 3781.500581 msec
00] formats the texts in uppercase. - Flag:
File size is 417455 bytes Elapsed Time is 3781.500581 msec
12 [left-justified],File size is 417455 bytes Elapsed Time is 3781.500581 msec
13 [include sign],File size is 417455 bytes Elapsed Time is 3781.500581 msec
14 [include leading space],File size is 417455 bytes Elapsed Time is 3781.500581 msec
15 [zero-padded],File size is 417455 bytes Elapsed Time is 3781.500581 msec
16 [include grouping separator].File size is 417455 bytes Elapsed Time is 3781.500581 msec
17 [negative value in parentheses],File size is 417455 bytes Elapsed Time is 3781.500581 msec
18 [alternative form].
Read JDK API
File size is 417455 bytes Elapsed Time is 3781.500581 msec19's "" for details on format specifiers.Examples
public boolean accept[File dirName, String fileName]5
public boolean accept[File dirName, String fileName]6
public boolean accept[File dirName, String fileName]7
public boolean accept[File dirName, String fileName]8
public boolean accept[File dirName, String fileName]9
public abstract int read[] throws IOException0
Formatted-Text Output via java.util.Formatter Class & format[] method
JDK 1.5 introduced
public void abstract void write[int unsignedByte] throws IOException7 for formatted text input. It also introduced
File size is 417455 bytes Elapsed Time is 3781.500581 msec19 for formatted text output.
A
public void abstract void write[int unsignedByte] throws IOException8 is an interpreter for
File size is 417455 bytes Elapsed Time is 3781.500581 msec23-style format strings. It supports layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output, via the format specifiers.
The
public void abstract void write[int unsignedByte] throws IOException8 has the following constructors:
public void abstract void write[int unsignedByte] throws IOException8 supports
File size is 417455 bytes Elapsed Time is 3781.500581 msec26/
File size is 417455 bytes Elapsed Time is 3781.500581 msec27 [as
File size is 417455 bytes Elapsed Time is 3781.500581 msec28] as output sink. It does not support
File size is 417455 bytes Elapsed Time is 61.834954 msec7, probably because
File size is 417455 bytes Elapsed Time is 61.834954 msec7 is immutable.
The
File size is 417455 bytes Elapsed Time is 3781.500581 msec0 method can be used to write formatted text output:
public abstract int read[] throws IOException1
Notice that the method
File size is 417455 bytes Elapsed Time is 3781.500581 msec0 has the same syntax as the method
public void abstract void write[int unsignedByte] throws IOException9, using the same set of format specifier as
public void abstract void write[int unsignedByte] throws IOException9.
Other methods are:
Example 1: Using a
File size is 417455 bytes Elapsed Time is 3781.500581 msec27 [which implements
File size is 417455 bytes Elapsed Time is 3781.500581 msec28] as the output sink for the
public void abstract void write[int unsignedByte] throws IOException8.
public abstract int read[] throws IOException2
Read JDK API
File size is 417455 bytes Elapsed Time is 3781.500581 msec19's "" for details on format specifiers.
Example 2: Setting the charset for
public void abstract void write[int unsignedByte] throws IOException8's output.
String.format[]
The
public void abstract void write[int unsignedByte] throws IOException8 with
File size is 417455 bytes Elapsed Time is 3781.500581 msec27 as the output sink allows you to build up a formatted string progressively. To produce a simple formatted
File size is 417455 bytes Elapsed Time is 61.834954 msec7, you can simply use the
File size is 417455 bytes Elapsed Time is 61.834954 msec5 method
File size is 417455 bytes Elapsed Time is 3781.500581 msec44. This is handy in the
File size is 417455 bytes Elapsed Time is 3781.500581 msec45 method, which is required to return a
File size is 417455 bytes Elapsed Time is 61.834954 msec7. For example,
public abstract int read[] throws IOException3
File I/O in JDK 1.7
[This section was extracted from the Java Online Tutorial and JDK 7 API.]
JDK 1.7 greatly enhances supports for file I/O via new package
File size is 417455 bytes Elapsed Time is 3781.500581 msec1 and its associated packages.
Interface java.nio.file.Path
A path string could be used to locate a file, a directory or a symbolic link. A symbolic link [or symlink] is a special file that references another file. A path string is system dependent, e.g., "
File size is 417455 bytes Elapsed Time is 3781.500581 msec8" in Windows or "
File size is 417455 bytes Elapsed Time is 3781.500581 msec9" in Unix. Windows uses back-slash
File size is 417455 bytes Elapsed Time is 2.938921 msec0 as the directory separator; while Unixes use forward-slash
File size is 417455 bytes Elapsed Time is 2.938921 msec1. Windows uses semi-colon
File size is 417455 bytes Elapsed Time is 2.938921 msec2 as path separator; while Unixes use colon
File size is 417455 bytes Elapsed Time is 2.938921 msec3. The "
File size is 417455 bytes Elapsed Time is 2.938921 msec7" or "
File size is 417455 bytes Elapsed Time is 2.938921 msec8" is called the root. Windows supports multiple roots, each maps to a drive [e.g., "
File size is 417455 bytes Elapsed Time is 2.938921 msec7", "
File size is 417455 bytes Elapsed Time is 61.834954 msec0"]. Unix has single root ["
File size is 417455 bytes Elapsed Time is 2.938921 msec8"]. A path could be absolute [beginning from the root] or relative [which is relative to the current working directory]. Special notations "
File size is 417455 bytes Elapsed Time is 61.834954 msec2" and "
File size is 417455 bytes Elapsed Time is 61.834954 msec3" denote the current directory and the parent directory, respectively.
A
File size is 417455 bytes Elapsed Time is 3781.500581 msec6 instance specifies the location of a file, or a directory, or a symbolic link.
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 replaces
File size is 417455 bytes Elapsed Time is 3781.500581 msec4 [of the standard I/O], which is less versatile and buggy.
Helper class java.nio.file.Paths
To create a
File size is 417455 bytes Elapsed Time is 3781.500581 msec62, use the
File size is 417455 bytes Elapsed Time is 61.834954 msec5 method
File size is 417455 bytes Elapsed Time is 3781.500581 msec66 of the helper class
File size is 417455 bytes Elapsed Time is 3781.500581 msec67. The helper class
File size is 417455 bytes Elapsed Time is 3781.500581 msec68 contains exclusively
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods for creating
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 objects.
File size is 417455 bytes Elapsed Time is 3781.500581 msec71 returns a
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 object by converting a given path string or
File size is 417455 bytes Elapsed Time is 3781.500581 msec73.
For example,
As the directory-separator is system dependent, for writing portable and more flexible program, it is recommended to use an existing
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 instance as an anchor to resolve the filename, e.g.,
A
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 can be broken down as root, level-0, level-1, .... The method
File size is 417455 bytes Elapsed Time is 3781.500581 msec76 returns the levels. The method
File size is 417455 bytes Elapsed Time is 3781.500581 msec77 returns the name of level-i. For example,
Helper Class java.nio.file.Files
The class
File size is 417455 bytes Elapsed Time is 3781.500581 msec78 contains exclusively
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods for file, directory and symlink operations such as create, delete, read, write, copy, move, etc.Properties of a File/Directory
You can use
File size is 417455 bytes Elapsed Time is 61.834954 msec5
public void abstract void write[int unsignedByte] throws IOException57 methods
File size is 417455 bytes Elapsed Time is 3781.500581 msec82 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec83 to verify if a given
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 exists or does not exist [as a file, directory or symlink]. A
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 could be verified to exist, or not exist, or unknown [e.g., the program does not have access to the file]. If the status is unknown, the
File size is 417455 bytes Elapsed Time is 3781.500581 msec86 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec87 returns
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]01.
You could also use
File size is 417455 bytes Elapsed Time is 61.834954 msec5
public void abstract void write[int unsignedByte] throws IOException57 methods
File size is 417455 bytes Elapsed Time is 3781.500581 msec91,
File size is 417455 bytes Elapsed Time is 3781.500581 msec92 and
File size is 417455 bytes Elapsed Time is 3781.500581 msec93 to verify whether a
File size is 417455 bytes Elapsed Time is 3781.500581 msec62 locates a file, directory, or symlink.
Many of these methods take an optional second argument of
File size is 417455 bytes Elapsed Time is 3781.500581 msec95, which is applicable for symlink only. For example,
File size is 417455 bytes Elapsed Time is 3781.500581 msec96 specifies do not follow the symlink.
For example, to get the size of a file in JDK 1.7:
Deleting a File/DirectoryYou can use
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods
File size is 417455 bytes Elapsed Time is 3781.500581 msec98 to delete a file or directory. Directory can be deleted only if it is empty. A
public void abstract void write[int unsignedByte] throws IOException57 method
File size is 417455 bytes Elapsed Time is 2.938921 msec00 is also available.
public abstract int read[] throws IOException4Copying/Moving a File/Directory
You can use
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods
File size is 417455 bytes Elapsed Time is 2.938921 msec02 or
File size is 417455 bytes Elapsed Time is 2.938921 msec03 to copy or move a file or directory. The methods return the target
File size is 417455 bytes Elapsed Time is 3781.500581 msec62.
The methods accepts an optional third argument of
File size is 417455 bytes Elapsed Time is 2.938921 msec05. For examples:
File size is 417455 bytes Elapsed Time is 2.938921 msec06 replaces the target if it exists;
File size is 417455 bytes Elapsed Time is 2.938921 msec07 copies the file attributes such as the dates;
File size is 417455 bytes Elapsed Time is 2.938921 msec08 specifies not to follow symlinks.
public abstract int read[] throws IOException5
To copy a file into another directory:
public abstract int read[] throws IOException6Reading/Writing Small Files
For small files, you can use
File size is 417455 bytes Elapsed Time is 61.834954 msec5 methods
File size is 417455 bytes Elapsed Time is 2.938921 msec10 [byte-based] and
File size is 417455 bytes Elapsed Time is 2.938921 msec11 [character-based] to read the entire file. You can use
File size is 417455 bytes Elapsed Time is 2.938921 msec12 [byte-based] or
File size is 417455 bytes Elapsed Time is 2.938921 msec13 [character-based] to write to a file.
The optional
File size is 417455 bytes Elapsed Time is 2.938921 msec14 includes:
File size is 417455 bytes Elapsed Time is 2.938921 msec15,
File size is 417455 bytes Elapsed Time is 2.938921 msec16,
File size is 417455 bytes Elapsed Time is 2.938921 msec17 [truncates the file to zero bytes],
File size is 417455 bytes Elapsed Time is 2.938921 msec18 [creates a new file and throws an exception if the file already exists],
File size is 417455 bytes Elapsed Time is 2.938921 msec19 [opens the file if it exists or creates a new file if it does not], among others.Byte-based OperationCharacter-based Operation
Examples: The following example write a small text file [in "UTF-8"], and read the entire file back as bytes as well as characters.
Buffered Character-based I/O for Text FilesFor Reading, use
File size is 417455 bytes Elapsed Time is 2.938921 msec20 method to open a text file, which returns a
public boolean accept[File dirName, String fileName]47. Use
File size is 417455 bytes Elapsed Time is 2.938921 msec22 to read a line,
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]11 to read a char, or
File size is 417455 bytes Elapsed Time is 2.938921 msec24 to read into a char-array.
For Writing, use the
File size is 417455 bytes Elapsed Time is 2.938921 msec25 method to open a output text file, which returns a
public boolean accept[File dirName, String fileName]48. Use
File size is 417455 bytes Elapsed Time is 2.938921 msec27 to write a character,
File size is 417455 bytes Elapsed Time is 2.938921 msec28 or
File size is 417455 bytes Elapsed Time is 2.938921 msec29 to write characters.
public abstract int read[] throws IOException7Byte-Based Stream I/O
Use
File size is 417455 bytes Elapsed Time is 2.938921 msec30 to allocate an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 for reading raw bytes; and
File size is 417455 bytes Elapsed Time is 2.938921 msec32 to allocate an
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 for writing. The
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]06 and
public String[] list[FilenameFilter filter] public File[] listFiles[FilenameFilter filter] public File[] listFiles[FileFilter filter]07 returned are not buffered.
Example: Similar to the previous program which read/write the entire file, this program read/write via Buffered I/O.
Creating a New File/Directory/SymlinkBeside using the
File size is 417455 bytes Elapsed Time is 2.938921 msec36 method with
File size is 417455 bytes Elapsed Time is 2.938921 msec14 of
File size is 417455 bytes Elapsed Time is 2.938921 msec19 or
File size is 417455 bytes Elapsed Time is 2.938921 msec18, you can also use
File size is 417455 bytes Elapsed Time is 2.938921 msec40 method to create an empty file. You can use the default file attributes or optionally define the initial attributes of the file.
public abstract int read[] throws IOException8
The
File size is 417455 bytes Elapsed Time is 2.938921 msec41 includes: [TODO]
- DOS:
- Unixes: Nine file permissions: read, write, and execute permissions for the file owner, members of the same group, and "everyone else", e.g., "
File size is 417455 bytes Elapsed Time is 2.938921 msec
42".
Example: [TODO]
Similarly, you can create a new directory, symlink as follows:
Example:
Creating a Temporary File/DirectoryExample: [TODO]
File Attributes
You can use one of the
File size is 417455 bytes Elapsed Time is 2.938921 msec43 methods to read all the basic attribute of a path.
FileChannel
Random Access File
The Interface
File size is 417455 bytes Elapsed Time is 2.938921 msec44 supports random access.
Directory Operations
List all root directoriesListing a directoryYou can list the contents of a directory by using the
File size is 417455 bytes Elapsed Time is 2.938921 msec45 method. The returned
File size is 417455 bytes Elapsed Time is 2.938921 msec46 object implements
File size is 417455 bytes Elapsed Time is 2.938921 msec47. You can iterate thru the entries with for-each loop.
public abstract int read[] throws IOException9
Example: List the contents of a directory
In addition, you can include a glob pattern to filter the entries.
public void abstract void write[int unsignedByte] throws IOException0
Example: List the contents of a directory filtered by a glob.
GlobA glob is a subset of regular expression.
File size is 417455 bytes Elapsed Time is 2.938921 msec
48 matches zero or more character [0+].File size is 417455 bytes Elapsed Time is 2.938921 msec
49 matches any one character.File size is 417455 bytes Elapsed Time is 2.938921 msec
50 behaves likeFile size is 417455 bytes Elapsed Time is 2.938921 msec
48, but can cross directory boundary, for matching on full path.File size is 417455 bytes Elapsed Time is 2.938921 msec
52 encloses a set of sub-patterns separated by ','.File size is 417455 bytes Elapsed Time is 2.938921 msec
53 encloses a set of single character or a range of character withFile size is 417455 bytes Elapsed Time is 3781.500581 msec
12, e.g., [aeiou], [a-z], [0-9].- Any other character matches itself. To match
File size is 417455 bytes Elapsed Time is 2.938921 msec
48,File size is 417455 bytes Elapsed Time is 2.938921 msec
49 or special characters, prefix withFile size is 417455 bytes Elapsed Time is 2.938921 msec
0.
For example: "
File size is 417455 bytes Elapsed Time is 2.938921 msec58" matches entry starts with "
File size is 417455 bytes Elapsed Time is 2.938921 msec59" and ends with "
File size is 417455 bytes Elapsed Time is 2.938921 msec60", "
File size is 417455 bytes Elapsed Time is 2.938921 msec61", or "
File size is 417455 bytes Elapsed Time is 2.938921 msec62".
You can also write your own codes to filter the entries.
public void abstract void write[int unsignedByte] throws IOException1
The interface
File size is 417455 bytes Elapsed Time is 2.938921 msec63 declares one
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6
public void abstract void write[int unsignedByte] throws IOException57 method
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];9, which will be call-back for each entry. Those entries that resulted in false
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];9 will be discarded.
public void abstract void write[int unsignedByte] throws IOException2
Example: The following program uses an anonymous instance of an anonymous
File size is 417455 bytes Elapsed Time is 2.938921 msec63 sub-class to filter the
File size is 417455 bytes Elapsed Time is 2.938921 msec46. The call-back method
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];9 returns
File size is 417455 bytes Elapsed Time is 2.938921 msec71 for regular files, and discards the rest. Take note that this filtering criterion cannot be implemented in a glob-pattern.
Walking the File Tree - Files.walkFileTree[]
You can use
File size is 417455 bytes Elapsed Time is 61.834954 msec5 method
File size is 417455 bytes Elapsed Time is 2.938921 msec73 ro recursively walk thru all the files from a starting directory.
First of all, you need to create an object that implements interface
File size is 417455 bytes Elapsed Time is 2.938921 msec74, which declares these
DataOutputStream out = new DataOutputStream[ new BufferedOutputStream[ new FileOutputStream["out.dat"]]];6 methods:
These methods return an
File size is 417455 bytes Elapsed Time is 2.938921 msec76 of
File size is 417455 bytes Elapsed Time is 2.938921 msec77, which could take values of
File size is 417455 bytes Elapsed Time is 2.938921 msec78,
File size is 417455 bytes Elapsed Time is 2.938921 msec79,
File size is 417455 bytes Elapsed Time is 2.938921 msec80,
File size is 417455 bytes Elapsed Time is 2.938921 msec81.
Instead of implementing
File size is 417455 bytes Elapsed Time is 2.938921 msec74 interface, you could also extend from superclass
File size is 417455 bytes Elapsed Time is 2.938921 msec83, and override the selected methods.
There are two versions of
File size is 417455 bytes Elapsed Time is 2.938921 msec84. The first version take a starting directory and a
File size is 417455 bytes Elapsed Time is 2.938921 msec74, and transverse through all the levels, without following the symlinks.
public void abstract void write[int unsignedByte] throws IOException3
Example:
The second version takes 2 additional arguments: the
File size is 417455 bytes Elapsed Time is 2.938921 msec86 specifies whether to follow symlink [e.g.,
File size is 417455 bytes Elapsed Time is 2.938921 msec87 or
File size is 417455 bytes Elapsed Time is 2.938921 msec88]; the
File size is 417455 bytes Elapsed Time is 2.938921 msec89 specifies the levels to visit [set to
File size is 417455 bytes Elapsed Time is 2.938921 msec90 for all levels].