Corresponding interfaces are defined for input/output in java. The I/O of java includes:
1. Byte stream
2. Character stream
3. File Stream
4. Object flow
5. Pipeline flow for communication between threads
Handling interface
1. DataInout and DataOutput interfaces for processing byte streams
2. ObjectInput and ObjectOutput interfaces for processing object streams
3. FileNameFilter interface for filtering file names
4. The Serializable interface that handles object streams. This object is an object serialization switch and does not contain any abstract methods or constants. Input/output objects must implement this interface before transmission.
abstract class
1. The abstract classes InputStream and OutputStream of byte stream
2. The abstract classes FilterOutputStream and FilterInputStream for filtering streams
3. The abstract classes Reader and Writer for character streams
4. Compress the abstract classes InflaterInputStream and DeflaterOutputStream
File class
RamdomAccessFile provides the function of random access to files. It inherits the Object class and implements the DataInput and DataOut interfaces. Therefore, the RandomAccessFile class has input and output functions and can access any location of the file. The RandomAccessFile class has the following common methods
1.length() gets the length of the file
2. seek() to find file records
3.close() closes the file resource
4. readDouble(), readInt(), readChar() to read file contents
5.skipBytes() skips the specified bytes
The file class only operates the attributes of the file on the disk, and the function of reading and writing file content needs to be provided by the stream class
byte stream
Byte input streams are all subclasses of InputStream, and they need to implement the DataInput interface. DataInputStream is a class that implements this interface. Byte output streams are subclasses of OutputStream and require the DataOutput interface. DataOutStream is a class that implements this interface.
Typical stream class in byte stream
1.DataInputStream
Create the FileInputStream class with the binary file as the constructor parameter, and create the DataInputStream class with the FileInputStream class as the constructor parameter
method:
read() data read in
available() gets the number of readable bytes
skip() skips reading bytes
close() closes the input stream
2.DataOutputStream
Create the FileOutputStream class with the binary file as the constructor parameter, and create the DataOutputStream class with the FileOutputStream class as the constructor parameter
method:
writeDouble();writeInt();writeChar();write() writes to the output stream
flush() empties the output buffer, forcing writing to the output stream
3.ByteArrayInputStream
Directly access memory, read data in byte array
Parent class: InputStream
方法:read();skip();available();markSupported();mark();reset();close()
4.ByteArrayOutputStream
Direct access to memory, write data to byte array
父类:OutputStream
method:
write();writeTo();reset();close();
toByteArray() copies the buffer contents to the newly created array
size() gets the current buffer size
toString converts the data content to an ENC encoded string
5.StringReader
read content as a string
Parent class: Reader
read() reads the string in the buffer and converts it to bytes