First, the concept of input and output
Refers to the operation of the program interacting with an external device or other computer;
Java implements these input and output operations with streams, which are represented by a unified interface, making the program design easier.
In or out is relative to the memory;
Read data into memory, called input, that is, input, the data read operation;
Writes data from memory to an external device, called output, which is the write operation of the data.
Second, the file class
1, the concept of the file class
The file class is a very important class in the java.io package;
The object of the file class can represent a file, and it can also represent a directory, in which a file class object can represent a document or directory;
The file object can manipulate the properties of files or directories, such as file name, last modified date, file size, etc.
The file object cannot manipulate the specific data of the files, that is, the file cannot be read/write directly.
2. How to construct file:
The method of constructing the file class has 4 overloaded methods, which are commonly used as follows:
such as://Create a file object associated with the Aaa.txt file name in the current directory
File F1 = new file ("Aaa.txt");
Indicate the detailed path and file name, and note the double slash or backslash
File F2 = new file ("D:\\java\\hello.java");
Indicate the detailed path and directory name, note the double slash
File F3 = new file ("D:\\java");
Iii. input stream and output stream
1, the flow according to the data transmission direction is divided into:
-Input stream: reads the input stream into memory;
-Output stream: Writes out of memory to be called the output stream.
2. All input streams are subclasses of the InputStream class or reader class
-Classes with class names ending in InputStream are subclasses of InputStream;
-Class name The class that ends with reader is a subclass of the reader class.
3. All output streams are subclasses of the OutputStream class or writer class
-Classes with class names ending in OutputStream are subclasses of OutputStream;
-Classes with the class name ending in writer are subclasses of the writer class.
Byte stream and character stream.
1, from the data stream encoding format is divided into
-byte stream
-Character Stream
2. Subclasses of InputStream and OutputStream are byte streams
-Can read and write binary files, mainly processing audio, pictures, songs, byte stream, processing unit is 1 bytes.
3. Reader and writer subclasses are character streams
-the main processing character or string, the character stream processing unit is 2 bytes;
-byte stream will read to the bytes data, go to the specified encoding table to get the corresponding text.
4. Common classes in byte stream
-byte input stream FileInputStream
-byte output stream FileOutputStream
5. Common classes in character stream
-Character input stream FileReader
-Character output stream FileWriter
Byte stream and character stream conversion
1, the origin of the conversion stream
-A bridge between a character stream and a byte stream
-facilitates operation between character stream and byte stream
2. Application of Conversion stream
-Convert to character stream operation is more efficient when the data in the byte stream is all characters
3. Conversion of bytes into a character stream bridge
-inputstreamreader
InputStreamReader (InputStream in)
InputStreamReader (InputStream in, String CharsetName)
-outputstreamwriter
OutputStreamWriter (OutputStream out)
OutputStreamWriter (OutputStream out, String charsetname)
It reads in bytes and converts it to a character stream based on the specified encoding.
The encoding used may be specified by name, or the platform can accept the default encoding method.
Six, Scanner class
The scanner class is located in the Java.util package and is not in the java.io package and is not part of the IO stream
Scanner is a tool class, the main goal is to simplify the scanning of text, most often use this class to get console input
Scanner steps to get console input
1. Create a Scanner object using console input
Scanner scanner=new Scanner (system.in);
2. Call the Nextxxx method in scanner to get the required data type
For example: Next, Nextline, Nextint, nextbyte, etc.
Summary of IO in the Java language