- 1. Input Stream
- 2. OutputStream
- 3. Reader
- 4. Writer
- 5. Standard IO Streams
- 6. Interfaces
- 7. File
- 8. Summary
- 9. Questions
- 10. References
This post will discuss content in Java IO streams. It is based Java SE 8.
int available(): return available to read the remaining byte size.
void close(): close the input stream.
void mark(int readlimit): marks the current position in this input stream.
readlimit: the number of bytes that can be read from the stream after setting the mark before the mark becomes invalid. Note the parameter for some subclass of
InputStreamhas no meaning.
boolean markSupported(): return if this input stream supports the mark and reset methods.
int read(): reads the next byte of data.
int read(byte b): reads some bytes from input stream and stores them into buffer array b.
int read(byte b, int off, int len): reads up to
lenbytes of data from input stream into array of bytes.
void reset(): repositions this stream to the position at mark (default 0)
long skip(long n): skips bytes of data.
InputStream only reads data from front to back. It can skip some data to read. It can back to specified position to read again.
InputStream can read bytes one by one or read some bytes into byte array once.
If there is no available data in
InputStream, the calling one of the read methods will return -1.
int read(byte b) =>
int read(byte b, int offset, int len),
abstract int read()
read() methods can implement directly (e.g ByteArrayInputStream) or implement by calling the native method (e.g FileInputStream).
ByteArrayInputStream: Byte array as data
FileInputStream: File as data
ObjectInputStream: Deserializes primitive data and objects previously written using an
PipedInputStream: A piped input stream should be connected to a piped output stream; the piped input stream then provides whatever data bytes are written to the piped output stream.
(Deprecated): String as data.
SequenceInputStream: Logical concatenation of other input streams.
SequenceInputStream(InputStream s1, InputStream s2)
FilterInputStream: It contains some other input stream as data, and transforming data or providing additional functionality.
BufferedInputStream: read data from the internal buffer array rather than read data from input stream every time.
PushbackInputStream: unread one byte.
DataInputStream: read primitive Java data types.
(Deprecated): provides the added functionality of keeping track of the current line number.
void close(): close output stream and release resources.
void flush(): Flushes output stream and forces buffered bytes to be written out.
void write(byte b): Writes byte array.
void write(byte b, int offset, int len): Writes
lenbytes from the byte array starting at offset.
void write(int b): Writes the specified byte.
Write one byte or some bytes data to output stream.
If output stream doesn’t call the flush or close method, some data written in buffer array may don’t write to the stream.
Some output streams have the buffer array, others not.
ByteArrayOutputStream: Writes data to a byte array. retrieve data using
FileOutputStream: Writes data to a File or
ObjectOutputStream: Writes primitive data types and java objects to an
PipedOutputStream: Sending data to pipe.
FilterOutputStream: Sit on top of an already existing output stream. It transforming the data or providing additional functionality.
BufferedOutputStream: Writes data to buffer array. Avoid write data to stream every time.
DataOutputStream: Write primitive Java data types to output stream.
PrintStream: Print various values to output stream.
if not implements Serializable interface, calling
writeObject() will throw
abstract void close()
void mark(int readAheadLimit)
int read(char cbuf)
abstract int read(char cbuf, int off, int len)
int read(CharBuffer target)
java.nio.CharBuffer targetis using for read characters.
boolean ready(): return whether this stream is ready to be read. It’s same with
long skip(long n)
Read one character to return or read some characters into char array from somewhere, e.g char array, file, pipe.
Writer append(char c)
Writer append(CharSequence csq)
- Same with
write(String str), but it can chained call, and String parameter can be null.
- Same with
Writer append(CharSequence csq, int start, int end)
abstract void close()
abstract void flush()
void write(char cbuf)
abstract void write(char cbuf, int offset, int len)
void write(int c)
void write(String str)
void write(String str, int offset, int len)
Writes one character or write some characters to somewhere, e.g char array, file, pipe.
OutputStreamWriter(OutputStream out, Charset cs)
void println(String s)
java.lang.AutoClosable: Auto close resources.
void close(): This method is invoked automatically on objects managed by the try-with-resources statement.
Closeable: resources can be closed.
DataInput: provide reading data in any of the Java primitive types.
DataOutput: provide writing data in any of the Java primitive types.
void writeBoolean(int b)
void writeInt(int val)
Serializable interface. It using for object serialization.
void readExternal(ObjectInput in)
void writeExternal(ObjectOutput out)
FileFilter: It is a functional interface and can be used for a lambda expression or method reference. Filter files.
boolean accpet(File pathname)
FileNameFilter: It is a functional interface and can be used for a lambda expression or method reference. Filter file names.
boolean accept(File dir, String name)
Flushable: data can be flushed.
void flush(): Flushes buffered output to the underlying stream.
DataInput interface. Allows reading object.
ObjectInputValidation: Callback interface to allow validation of objects within a graph. It has no implemented subclasses.
DataOutput interface. Allows writing object.
void writeObject(Object obj)
ObjectStreamConstants: Constants written into the Object Serialization Stream.
Serializable: Allows class serializability. It has no methods.
static String pathSeparator: system dependent path separator, e.g
static String separator: system dependent name separator, e.g
File(File parent, String child),
File(String parent, Sring child),
- File Permission
boolean setExecutable(boolean executable),
boolean setWritable(boolean writable)
- File Handling
static File createNewFile(),
static File createTempFile(String prefix, String suffix, File directory),
int compareTo(File pathname): compares two abstract pathnames lexicographically.
String list(FilenameFilter filter),
- File Information
Whenever a file is opened, the operating system creates an entry to represent this file and stores its information. Each entry is represented by an integer value and this entry is termed as file descriptor. 
Basically, Java class
FileDescriptorprovides a handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. 
The applications should not create FileDescriptor objects, they are mainly used in creation of
FileOutputStreamobjects to contain it. 
Get file, absolute filepath and inputStream in classpath, e.g
You can get InputStream by using ClassLoader.getResourceAsStream() method.
InputStream in = getClass().getClassLoader().getResourceAsStream("test.txt");
You can get file object and file path by using ClassLoader get URL, then get them.
URL url = getClass().getClassLoader().getResource("test.txt");
getClass().getClassLoader().getResource("test.txt") //relative path
Get file, absolute file path and inputStream in JAR, e.g src/resources/test.txt
This is deliberate. The contents of the “file” may not be available as a file. Remember you are dealing with classes and resources that may be part of a JAR file or other kind of resource. The classloader does not have to provide a file handle to the resource, for example the jar file may not have been expanded into individual files in the file system. 
Anything you can do by getting a java.io.File could be done by copying the stream out into a temporary file and doing the same, if a java.io.File is absolutely necessary. 
Result: You can get input stream and File by ClassLoader, but you can’t get right usable absolute file path. If you want get a usable absolute path in JAR, you can copy resource file stream to create a new temporary file, then get absolute file path of the temporary file.
File file = null;
Reader it read data from somewhere, e.g byte or char array, file, pipe. It can read single-unit data from the stream to return or read multiple units data from the stream to store into an array every time.
read() methods can implement by java code or implements by calling native method. Filter streams use the Decorator Pattern to add additional functionality (e.g unread) that implements by internal buffer array to other stream objects.
Writer it write data to somewhere, e.g byte or char array, file, pipe.
- Byte or Char Array
- Buffered: Operating with buffer array.
- DataInput, DataOutput: Operating with primitive Java types
- Pushback: Add unread functionality.
class java.nio.CharBuffer using in method
int read(CharBuffer target)
interface java.lang.CharSequence using in method
Writer append(CharSequence csq)
InputStream vs Reader? What the real difference between them? Who has more efficient?
Stream is operating bytes, and reader or writer is operating characters. Writing streams of raw bytes such as image data using OutputStream. Writing streams of characters using Writer.
CharSequence vs String
CharSequence is a readable sequence of
char values. You can call
chars() method get the InputStream.