1.java.io under the Bag
File class: An object of this class in a Java program that corresponds to a resource in a file or network.
Flie file1 = new File ("D:\\io\\hello.txt");
File File2 = new file ("D:\\io\\io1");
>1. File can represent either a document or a file directory
>2. The object of file is platform-Independent
>3. The file class is for files or file directories, only new, deleted, renamed, upper-level directories, and so on. When it comes to accessing the file content, it can only be manipulated using IO stream.
>4. The object of the file class is often passed as a parameter to the constructor of the corresponding input and output stream.
Structure of the 2.IO stream
Division of 3.IO Streams
1) Follow flow direction: input and output (stand at the program angle)
2) varies by unit of data in stream: Byte stream stream (plain text file uses character stream in addition to byte stream)
3) Depending on the role of the stream: node stream processing flow (flow directly on the file is the node stream, in addition to the processing flow)
4. Key to mastering
* Abstract base class node stream (file stream) buffer stream (one of the processing streams that can improve the efficiency of file operations)
* InputStream fileinputstream (int read (byte[] b)) Bufferedinputstream (int read (byte[] b))
* OutputStream FileOutputStream (void Write (B,0,len)) Bufferedoutputstream (Flush ()) (void Write (B,0,len))
* Reader filereader (int read (char[] c)) BufferedReader (ReadLine ()) (int read (char[] c)) or string ReadLine ()
* Writer FileWriter (void Write (C,0,len)) BufferedWriter (Flush ()) (void Write (C,0,len) or void write (Strin G str))
Note: Reading a file from the hard disk requires that the file must exist. If it does not exist, report the abnormal FileNotFoundException
Enter a file from the program to the hard disk, this file can not exist. If it does not exist, create one and implement the output. Overwrite if present.
In real development, the buffer stream is used instead of the node stream.
The main end is to close the corresponding stream, first close the output stream, and then close the input stream. This action is put in the finally.
5. Other flows
1. Convert stream: Implements conversion between byte stream and character stream
InputStreamReader: When input, implement the conversion of byte stream to character streams, improve the efficiency of operation (if data is a text file) =====> decode: byte array to string
OutputStreamWriter: The conversion of the character stream to the byte-stream is implemented when output. ===> encoding: String to byte array
Examples:
Entering a string from the keyboard requires that the entire line of the read string be converted to an uppercase output. Then proceed with the input operation until you exit the program when you enter "E" or "exit".
2. Standard input/output stream
System.in:The "standard" input stream: Entering data from the keyboard
System.out:The "standard" output stream: outputting data from the monitor
3. Print Flow
4. Data Flow
5. Object Flow
6. Random Access file stream
Java Learning diary-----IO Stream