In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the IO flow knowledge points of Java". In the daily operation, I believe that many people have doubts about the IO flow knowledge points of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the knowledge points of Java IO flow?" Next, please follow the editor to study!
Category 1: by mode of operation (class structure)
Byte stream and character stream:
Byte stream: in bytes, each read or read is 8 bits of data. You can read any type of data.
Character stream: in characters, each read or read is 16-bit data. It can only read character type data.
Output stream and input stream:
Output stream: read from memory to file. Only write operations can be performed.
Input stream: read from file to memory. Only reads can be performed.
Note: the output and input here are relative to the system memory.
Node flow and processing flow:
Node stream: directly connected to the data source, read in or read out.
Processing flow: used with the node flow, and then nesting one layer on the basis of the node flow, the processing flow is connected to the node flow.
Why should there be a processing flow? Directly use the node stream, it is not convenient to read and write, in order to read and write files faster, there is a processing stream.
Classify the structure diagram according to the mode of operation:
Based on the above classification and the description of jdk, we can draw a more detailed class structure diagram, as follows:
Classification description
1. Enter byte stream InputStream:
The inheritance diagram of the input byte stream can be seen in the above figure, which can be seen as follows:
FileInputStream: three basic media streams that read data from the Byte array, StringBuffer, and local files, respectively.
ByteArrayInputStream:
PipedInputStream: reads data from pipes that are shared with other threads. An instance of PipedInputStream is used together with an instance of PipedOutputStream to complete the read and write operation of the pipeline. It is mainly used for thread operation.
ObjectInputStream and all subclasses of FilterInputStream are decoration streams (the protagonist of the decorator pattern)
two。 Output byte stream OutputStream:
The inheritance diagram of the output byte stream is shown in the above figure, which can be seen as follows:
FIleOutputStream: two basic media streams
ByteArrayOutputStream: two basic media streams that write data to the Byte array and to the local file, respectively.
PipedOutputStream: writes data to pipes that are shared with other threads.
ObjectOutputStream and all subclasses of FilterOutputStream are decoration streams.
Comparison of input and output of byte stream:
3. Character input stream Reader:
As you can see in the inheritance diagram above:
FileReader:
PipedReader: reads data from pipes that are shared with other threads
CharArrayReader:
CharReader and StringReader are two basic media streams, which read data from Char array and String respectively.
BufferedReader is clearly a decorator, and it and its subclasses are responsible for decorating other Reader objects.
FilterReader is the parent class of all custom concrete decoration streams, and its subclass PushbackReader decorates the Reader object with a line number.
InputStreamReader: a bridge between a byte stream and a character stream that converts a byte stream into a character stream. FileReader can be said to be a commonly used tool class to achieve this function, and its source code clearly uses the method of turning FileInputStream into Reader. We can get some skills from this category. The purpose and usage of each class in Reader is basically the same as that of classes in InputStream. There will be a correspondence between Reader and InputStream.
4. Character output stream Writer:
As can be seen in the diagram above:
FileWriter:
PipedWriter: writes data to pipes that are shared with other threads
CharArrayWriter:
CharArrayWriter and StringWriter are two basic media streams, which write data to Char array and String respectively.
BufferedWriter is a decorator that provides buffering for Writer.
PrintWriter and PrintStream are very similar, and their functions and usage are very similar.
OutputStreamWriter: it is the bridge from OutputStream to Writer, and its subclass FileWriter is actually a concrete class that implements this function (specifically, you can study-SourceCode). The function and use are very similar to OutputStream, and their corresponding diagrams will be shown later.
Comparison of input and output of character stream:
5. Conversion between character stream and byte stream
Characteristics of the conversion flow:
It is the bridge between character stream and byte stream.
The read byte data can be converted into characters by specified encoding.
The read character data can be converted to bytes by specified encoding.
When to use the conversion stream?
When there is a conversion action between bytes and characters
When the data of a stream operation needs to be encoded or decoded.
Specific implementation:
InputStreamReader: input stream to read stream
String fileName= "d:" + File.separator+ "hello.txt"; File file=new File (fileName); Writer out=new OutputStreamWriter (new FileOutputStream (file)); out.write ("hello"); out.close ()
OutputStreamWriter: transfer the output stream to the write stream
String fileName= "d:" + File.separator+ "hello.txt"; File file=new File (fileName); Reader read=new InputStreamReader (new FileInputStream (file)); char [] b=new char [100]; int len=read.read (b); System.out.println (new String); read.close (); these two stream objects are members of the character system, they have the function of transformation, and they are character streams themselves, so the byte stream object needs to be passed in during construction. Category 2: by object of operation
Classify the structure diagram by operation object:
Classification description:
Manipulate the file (node flow):
FileInputStream (byte input stream)
FileOutputStream (byte output stream)
FileReader (character input stream)
FileWriter (character output stream)
Operate on the pipe (node flow):
PipedInputStream (byte input stream)
PipedOutStream (byte output stream)
PipedReader (character input stream)
PipedWriter (character output stream).
An instance of PipedInputStream is used together with an instance of PipedOutputStream to complete the read and write operation of the pipeline. It is mainly used for thread operation.
Byte / character array stream (node stream):
ByteArrayInputStream
ByteArrayOutputStream
CharArrayReader
CharArrayWriter
Is to open a byte or character array in memory.
Except for the above three are node flows, the others are processing flows and need to be used in conjunction with node flows.
Buffered buffer flow (processing flow):
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
Is the processing flow with buffer, the main purpose of the buffer is to avoid dealing with the hard disk every time and improve the efficiency of data access.
Conversion flow (processing flow):
InputStreamReader: convert bytes to characters
OutputStreamWriter: converts bytes into characters.
Base type data flow (processing flow): used to manipulate basic data type values.
DataInputStream
DataOutputStream .
Because what if we output an 8-byte long type or a 4-byte float type? You can output byte by byte, or you can convert it to string output, but it takes time to convert, so it would be nice to output directly, so this data stream solves the difficulty of outputting data types. Data flow can output float type or long type directly, which improves the efficiency of data reading and writing.
Print stream (processing stream):
PrintStream
PrintWriter
It is usually printed to the console, where you can control the printing.
Object flow (processing flow):
ObjectInputStream, object deserialization
ObjectOutputStream, object serialization
Output the encapsulated objects directly, rather than converting them into strings one by one.
Merge flow (processing flow):
SequenceInputStream: can be thought of as a utility class that reads two or more input streams as one input stream in turn.
Typical use ca
1. Copy the file:
/ * copy file: read and write * / class hello {public static void main (String [] args) throws IOException {if (args.length! = 2) {System.out.println ("incorrect input of command line parameters, please check"); System.exit (1);} File file1 = new File (args [0]) File file2 = new File (args [1]); if (! file1.exists ()) {System.out.println ("copied file does not exist"); System.exit (1);} InputStream input = new FileInputStream (file1); OutputStream output = new FileOutputStream (file2) If ((input! = null) & & (output! = null)) {int temp = 0; while ((temp = input.read ())! = (- 1)) {output.write (temp);}} input.close (); output.close ();}}
Description:
After using the stream, be sure to perform a close operation, that is, call the close () method.
FileInputStream.read ():
This method reads the stream byte by byte, and the result is the int representation of the byte.
When there is no content, the result returned is-1
FileOutputStream.write ():
Write the content to a file.
two。 Convert the characters in the stream to uppercase and lowercase without using FIle:
Public static void main (String [] args) throws IOException {String str = "ROLLENHOLT"; ByteArrayInputStream input = new ByteArrayInputStream (str.getBytes ()); ByteArrayOutputStream output = new ByteArrayOutputStream (); int temp = 0; while ((temp = input.read ())! =-1) {char ch = (char) temp; output.write (Character.toLowerCase (ch)) } String outStr = output.toString (); input.close (); output.close (); System.out.println (outStr);}
Description:
After using the stream, be sure to perform a close operation, that is, call the close () method.
3. Use pipe flow to communicate between multiple threads:
/ * messaging class * * / class Send implements Runnable {private PipedOutputStream out = null; public Send () {out = new PipedOutputStream ();} public PipedOutputStream getOut () {return this.out;} public void run () {String message = "hello, Rollen"; try {out.write (message.getBytes ()) } catch (Exception e) {e.printStackTrace ();} try {out.close ();} catch (Exception e) {e.printStackTrace ();}} / * accept message class * / class Recive implements Runnable {private PipedInputStream input = null; public Recive () {this.input = new PipedInputStream () } public PipedInputStream getInput () {return this.input;} public void run () {byte [] b = new byte [1000]; int len = 0; try {len = this.input.read (b);} catch (Exception e) {e.printStackTrace ();} try {input.close () } catch (Exception e) {e.printStackTrace ();} System.out.println ("accepted content" + (new String (b, 0, len));}} / * Test class * / class hello {public static void main (String [] args) throws IOException {Send send = new Send (); Recive recive = new Recive () Try {/ / pipe connection send.getOut () .connect (recive.getInput ());} catch (Exception e) {e.printStackTrace ();} new Thread (send) .start (); new Thread (recive) .start ();}}
4. Use the buffer to read from the keyboard:
Public static void main (String [] args) throws IOException {BufferedReader buf = new BufferedReader (new InputStreamReader (System.in)); String str = null; System.out.println ("Please enter content"); try {str = buf.readLine ();} catch (IOException e) {e.printStackTrace () } System.out.println ("what you entered is:" + str);}
5. Direct system output to a file:
Public static void main (String [] args) throws IOException {File file = new File ("/ Users/liuluming/Documents/hello.txt"); / / now output directly to the screen System.out.println ("hello"); try {System.setOut (new PrintStream (new FileOutputStream (file);} catch (FileNotFoundException e) {e.printStackTrace () } System.out.println ("these contents can only be seen in the file!") Other classes: File
The File class is an object that encapsulates files and folders in the file system, and you can manipulate files and folders through the idea of objects. The File class stores all kinds of metadata information about a file or directory, including file name, file length, last modification time, readability, getting the path name of the current file, judging whether the specified file exists, getting the list of files in the current directory, creating and deleting files and directories, and so on.
Other classes: RandomAccessFile
The object is not a member of the stream system, it encapsulates the byte stream, but also encapsulates a buffer (character array) that manipulates the data in the character array through internal pointers. The characteristics of the object:
This object can only manipulate files, so the constructor takes two types of parameters: a. String file path; b.File object.
The object can read and write to the file, and the operation mode can be specified when the object is instantiated (rforce RW).
Note:
When the object is instantiated, if the file to be operated on does not exist, it will be created automatically; if the file exists and the write data does not specify a location, it will be written from scratch, that is, the original content will be overwritten. Can be used for multi-threaded download or multiple threads to write data to the file at the same time, the "what are the IO stream knowledge points of Java" is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.