In Java Input output operations are based on the concept of stream. The java.io package contains all the required classes to perform I/O operations.
A stream is defined as a sequence of data.
Automatically created streams in Java:
- System.in: the standard input stream.
- System.out: standard output stream.
- System.err: standard error.
Input stream:
InputStream is used to read data from a source like a file, peripheral device, etc.
InputStream class:
InputStream class is the superclass of all classes representing an input stream. It is an abstract class.
Commonly used methods of InputStream class:
1. read(): Reads the next byte of data from the input stream. It returns -1 at the end of the file.
Syntax: public abstract int read()throws IOException.
2. available(): Returns an estimate of the number of bytes that can be read from the current input stream.
Syntax: public int available()throws IOException.
3. close(): It is used to close the current input stream.
Syntax: public void close()throws IOException.
Output Stream:
OutputStream is used for writing data to a destination like a file, peripheral device, etc.
OutputStream class:
OutputStream class is the superclass of all classes representing an output stream. It is an abstract class.
Commonly used methods of OutputStream class:
1. write(int): This method is used to write a byte to the current output stream.
Syntax: public void write(int)throws IOException.
2. write(byte[]): This method is used to write an array of bytes to the current output stream.
Syntax: public void write(byte[])throws IOException.
3. flush(): flushes the current output stream.
Syntax: public void flush()throws IOException.
4. close(): This method is used to close the current output stream.
Syntax: public void close()throws IOException.
Java input output stream tutorial:
- FileInputStream and FileOutputStream in java with example.
- Byte Streams in java with example.
- DataInputStream and DataOutputStream in java with example.
- BufferedInputStream and BufferedOutputStream in java with example.
- FileReader and FileWriter in java with example.
- How To Check If A File Exists In Java.
- Serialization in java with example.
- Transient in java with example.
- List all file names from directory java.
- Read all files from folder java.
- Filter the files by file types java.
- Read file content in byte array java.
- Read file content line by line java.
- Get file list from a folder filtered by extensions java.
- Get file uri reference java.
- Store and read objects from a file java.
- Create and store property file dynamically java.
- Store property file as xml file in java.
- Get file last modified time java.
- Convert byte array to inputstream java.
- Convert inputstream to bufferedreader java.
- Convert byte array to bufferedreader java.
- Set file permissions in java.
- Create temporary file in java.
- Store data into temporary file in java.
- Delete temporary file in java.
- Write string content to file java.
- Write byte content to a file java.
- Delete file in java program.
- Rename file in java program.
- Make a file read only in java.
- Check if file is writable java.
- Make a read only file writable in java.
- Check if a file is hidden in java.