The IO stream in Java is divided into two main categories:
- BYTE stream: Inputstream+outputstream
- Character Stream: Reader+writer
BYTE stream:
- InputStream is the parent class of all byte input streams
- OutputStream is the parent class of all byte output streams
- FileInputStream reading data from a file
- Fileoutpustream writing data to a file
- Bytearrayinputstream reading data from a byte array
- Bytearrayoutputstream writing data to memory
- PrintStream is a wrapper class that outputs data to a specified stream
- System.in is an instance of the InputStream subclass that specifies reading data from the keyboard (can be redirected)
- System.out/system.err is an instance of PrintStream that specifies that data is written to the screen (can be redirected)
Character Stream:
- Reader all character input stream parent class
- Writer all character output stream parent class
- FileReader reading a string from a file
- FileWriter outputting A string to a file
- InputStreamReader reading characters from the InputStream
- OutputStreamWriter character output to OutputStream
- BufferedReader reading characters from a specified reader
- BufferedWriter writes a character to the stream specified by writer
Here's an example of reading data from the keyboard and outputting it to the screen (using BufferedReader implementation)
ImportJava.util.*;ImportJava.io.*; Public classhello{ Public Static voidMain (string[] args) {bufferedreader br=NewBufferedReader (NewInputStreamReader (system.in)); Try{
System.out.print ("Please enter your message:"); String Line=Br.readline (); System.out.print (line); }Catch(IOException IoE) {ioe.printstacktrace (); } }}
Operation Result:
Please enter your Message:hello guys and nice to meet. My back hurts.
Hello guys, nice to meet. My back hurts.
Summary of IO in Java