First, the flow
Java implements I/O through a stream, which is an abstraction that can produce or consume information.
Java defines two types of streams:
- BYTE stream: Handles input and output of bytes, such as reading and writing binary data.
- Character Stream: Handles the input and output of characters.
All I/O at the bottom is still byte-oriented, and the character stream knowledge provides a more efficient way to process characters.
Second, the Word throttle class
|
Bufferedinputstream
Bufferedoutputstream |
Buffered input stream
Buffered output stream |
|
Bytearrayinputstream
Bytearrayoutputstream |
Reading the input stream of a byte array
Writes the output stream of a byte array |
|
DataInputStream
DataOutputStream |
Input stream for methods that read standard data types
Output stream for methods that write to standard data types |
|
FileInputStream
FileInputStream |
Read the input stream of a file
Output stream written to file |
|
InputStream
OutputStream |
Abstract class describing stream input
Abstract class describing stream output |
|
FilterInputStream
Filteroutputstream |
Implement InputStream
Implement OutputStream |
|
ObjectInputStream
ObjectOutputStream |
The input stream of the object
The output stream of the object |
|
PipedInputStream
PipedOutputStream |
Input piping
Output piping |
|
PrintStream |
Output streams that contain print and println |
|
Pushbackinputstream |
Allow bytes to return an input stream |
|
Sequenceinputstream |
An input stream composed of multiple input streams that are read sequentially |
Third, character Stream class
|
BufferedReader
BufferedWriter |
Buffered input character stream
Buffered output character stream |
|
CharArrayReader
Chararraywriter |
An input stream that reads content from a character array
Output stream that writes content to a character array |
|
FileReader
FileWriter |
Read the input stream of a file
Output stream written to file |
|
FilterReader
Filterwriter |
Filtered Reader
Filters for Writers |
|
InputStreamReader
OutputStreamWriter |
Input stream to convert bytes to characters
Output stream to convert characters to bytes |
|
StringReader
StringWriter |
Input stream to read content from a string
Output stream to write content to a string |
|
Pipedreader
PipedWriter |
Input piping
Output piping |
|
Reader
Writer |
Abstract class describing the character stream input
Abstract class describing the output of a character stream |
|
PrintWriter |
Output streams that contain print and println |
|
Pushbackreader |
Allow characters to return an input stream |
|
LineNumberReader |
Input streams that support row operations |
Iv. pre-defined flow
Predefined flows contained by system
- System.out: Standard output stream, PrintStream type object.
- System.err: Standard error stream, PrintStream type object.
- System.in: Standard input stream, InputStream type object.
V. Console
ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;classSolution { Public Static voidMain (string[] args)throwsIOException {BufferedReader reader=NewBufferedReader (NewInputStreamReader (system.in)); Charc = (Char) Reader.read ();//Get characterSystem.out.write (c); String Str= Reader.readline ();//reading a stringPrintWriter writer =NewPrintWriter (System.out,true); Writer.println (str); }}View Code
Vi. Read and write files
ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;classSolution {Staticstring Read (string fileName) {fileinputstream fin=NULL; StringBuilder Builder=NewStringBuilder (); Try{fin=NewFileInputStream (fileName); inti; Do{i= Fin.read ();//Read bytes if(I! =-1)//-1 means end of file reachedBuilder.append ((Char) (i); } while(I! =-1); } Catch(FileNotFoundException exc) {System.out.println ("Cannot open File"); } Catch(IOException exc) {System.out.println ("Cannot read file"); } finally { Try { if(Fin! =NULL) Fin.close (); } Catch(IOException exc) {System.out.println ("Cannot close file"); } } returnbuilder.tostring (); } Static voidWrite (string fileName, string content) {FileOutputStream Fout=NULL; Try{fout=NewFileOutputStream (fileName); for(inti = 0; I < content.length (); i++) Fout.write (int) Content.charat (i));//Write Bytes}Catch(FileNotFoundException exc) {System.out.println ("Cannot open File"); } Catch(IOException exc) {System.out.println ("Cannot write file"); } finally { Try { if(Fout! =NULL) Fout.close (); } Catch(IOException exc) {System.out.println ("Cannot close file"); } } }}View Code
Automatically closes the file, leaving the code block implicitly calling the Close method to close the stream associated object.
ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;classSolution { Public Static voidMain (string[] args) {Try(FileInputStream fin =NewFileInputStream ("file.txt")) { inti; Do{i=Fin.read (); if(I! =-1) System.out.print (Char) (i); } while(I! =-1); } Catch(FileNotFoundException exc) {System.out.println ("Cannot open File"); } Catch(IOException exc) {System.out.println ("Cannot read file"); } }}View Code
Java notes: I/O