Java File Class Tutorial
This Chapter discusses the details of reading, writing, creating, and opening files in java programming. The java.io package contains the types required to perform input/output operations in Java. There are a wide array of file I/O methods to choose from. Java's Input/Output (I/O) libraries are designed in an abstract way that enables you to read from external source of data sources and write to external targets, regardless of the kind of thing you’re writing to or reading from. When you work with files for I/O, you basically work with streams .What Is a Stream?
A stream may be defined as a sequence of bytes . Input streams transfer bytes of data into a Java program from external resource, while Output streams transfer bytes of data from Java to some external target. The stream classes in Java, such as FileInputStream , are byte-oriented. They read and write data one byte at a time or in groups of bytes.The Stream Classes
Most of the classes that work directly with streams are part of the java.io package. The two main classes are java.io.InputStream and java.io.OutputStream . These are abstract base classes for many different subclasses with more specialized abilities.
JDK has two sets of I/O packages:
- Standard I/O (in package java.io), introduced since JDK 1.0 for stream-based I/O
- New I/O (in packages java.nio), introduced in JDK 1.4, for more efficient buffer-based I/O.
Input operations are separated in terms of reading from a stream in a three-step process:
- Open the stream
- Read data items from the stream front to back in sequence
- Close the stream.
Output operations are separated in terms of writing to a stream in a three-step process:
- Open the stream
- Write data onto the end of the stream in sequence
- Close the stream.
From the following chapters, you can learn these classes in detail.
Related Topics