First of all, it is recommended to directly consider the character stream (Reader\Writer) for text data
Non-text can only use byte stream (InputStream\OutputStream)
---------------------------------------------------------------------------------
Pipe flow.
PipedInputStream
PipedOutputStream
Features: The read pipe stream and the write pipe stream can be connected.
Connection methods: 1: Through the constructors of the two stream objects; 2: Through the connect method of the two objects.
Usually, when two streams are used, multi-threading technology needs to be added, that is, to allow read and write to run at the same time.
Note: For the read() method. The method is blocking, that is, when there is no data, the method will also wait.
---------------------------------------------------------------------------------
serialization of objects
ObjectInputStream
ObjectOutputStream
Existing objects can be directly manipulated through these two stream objects and the objects can be persisted locally.
The stored object can be transmitted over the network.
Specific methods for both objects:
ObjectInputStream Object readObject():
The method throws an exception: ClassNotFountException;
ObjectOutputStream void writeObject(Object);
The object being written must implement an interface: Serializable otherwise it will throw: NotSerializableException
Serializable : This interface is actually a marker interface without methods.
Used to assign a UID to a class. The UID is a long value computed from the digital signature of the serializable member in the class.
As long as these members do not change, the value is the same every time the operation is performed.
This value is used to determine whether the serialized object is compatible with the class file.
If the serialized object needs to be compatible with different class versions. UIDs can be customized in the class.
Definition: static final long serialVersionUID = 42L;
Note: For static member variables, they will not be serialized.
For members that are not static and do not want to be serialized, they can be modified by the transient keyword.
Usually, these two objects are used in pairs.
---------------------------------------------------------------------------------
Manipulate stream objects of primitive data types
DataInputStream(InputStream);
method:
int readInt(); Reads four bytes at a time and converts them to an int value.
boolean readBoolean(): Reads one byte at a time.
short readShort();
long readLong();
There are others. . .
Sting readUTF(); Read characters according to the modified version of utf -8. Note that it only reads character data written by writeUTF().
DataOutputStream(OutputStream);
method:
writeInt( int ): Writes four bytes at a time. Note that unlike write( int ), write( int ) only writes the lowest 8 bits of the integer. The remaining three 8 bits are discarded.
writeBoolean(boolean);
writeShort(short);
writeLong(long);
There are others. . .
writeUTF(String): Store character data as utf -8 modified version. Can only be read via readUTF
Usually occurs in pairs; usually only basic data types are manipulated. It needs to be wrapped by DataStream.
-----------------------------------------------------------------------------------
Stream objects for manipulating arrays
byte array
ByteArrayInputStream
ByteArrayOutputStream
character array
CharArrayReader
CharArrayWriter
For these streams, the source is memory and the destination is memory
And these streams do not invoke system resources. An array in memory is used. So these objects do not need to be closed.
When constructing a read stream that operates on an array, a data source must be specified. So you need to pass in the corresponding array
For write streams that operate on arrays, null parameters can be used in the constructor. Because it has a variable length array built in as a buffer.
The emergence of these streams is actually the manipulation of arrays through the idea of reading and writing streams.
Similar objects do the same:
StringReader
StringWriter
-------------------------------------------------------------------------------------
RandomAccessFile:
The object is not a member of the stream system.
The object encapsulates the byte stream, and also encapsulates a buffer (byte array), and operates the data in the array through the internal pointer.
The object features:
1: The object can only operate on files, so the constructor accepts two types of parameters.
a: string path.
b: File object.
2: The object can both read and write to the file.
When instantiating an object, you must specify the operation mode of the object, r rw , etc.
This object has methods that can directly manipulate primitive data types.
The most characteristic method of this object:
skipBytes(): Skip the specified number of bytes.
seek(): Specifies the position of the pointer.
getFilePointer(): Gets the position of the pointer.
Through these methods, random access to a file data can be accomplished
Read wherever you want to read, and write where you want to write.
This object function can read data and write data. If there is data in the write location, data overwriting will occur.
That is, the data can be modified.
When using this object, it is suggested that the data be regular. Or segmented.
Note; when the object is instantiated, if the file to be operated does not exist, it will be created automatically.
If the file to be manipulated exists, it will not be created, if the existing file has data.
Then, if there is no specified location, writing data will overwrite the data at the beginning of the file.
It can be used for multi-threaded download, that is, to store data in a file at the same time through multi-threading.
-------------------------------------------------------------------------------------
sequence flow. Also known as merged stream.
SequenceInputStream:
Features: Multiple read streams can be combined into one stream. This is very convenient to operate.
Principle: In fact, each read stream object is stored in a collection. The last stream object ends as the end of this stream.
Two constructors:
1:SequenceInputStream(InputStream in1,InputStream in2)
Two read streams can be combined into one stream.
2:SequenceInputStream(Enumeration<?extends InputStream> en)
Multiple streams in an enumeration can be combined into a single stream.