JAVA data stream Overview
Considering the diversity of data sources, in order to effectively perform data input and output operations, data transmission between different data sources and programs is represented as "Stream ), to achieve relatively unified and simple input/Output (Inout/Output, I/O) operations.
Next let's take a look at the java data stream Overview:
Stream overview stream classification:
• Data Flow
-Input stream: Only bytes of data can be read from it, but data cannot be written to it.
-Output stream: Only bytes of data can be written to it, but data cannot be read from it.
• Data Types processed by stream
-Byte stream: used to process byte data.
-Character stream: used to process UNICODE character data
• Source processed by stream
-Node stream: the stream that reads/writes data from/to a specific IO Device.
-Stream processing: Connection and encapsulation of existing streams
Filter stream
Abstract inputstream and outputstream classes allow us to read and write strings and numbers. For this purpose, more sub-classes are required. For example, dateinputstream and dataoutputstream allow us to read and write all basic java types.
The file stream class is similar to the abstract inputstream and outputstream classes. These classes only support byte-level read/write operations. In other words, you can only read character and byte arrays from the fin object. Byteb = fin. Read (), they do not provide a special numeric type, so datainputstream cannot get a number from a file.
Solution: Java divides the responsibilities of a stream. Some streams (fileinputstream) are responsible for reading byte data from files or other special places. Other streams
(Datainputstream, printwriter) is responsible for "assembling" bytes into more useful data types. These two streams must be integrated into the so-called "filteredstreams" by hand to the builder of another stream.
Data Stream
L datainputstream
L dataoutputstream
For the class structure and members, see the documentation.
// Write and read the name and salary in the file
ImportJava. Io .*;
Public classT1 {
Public static voidMain (string [] ARGs ){
String [] Name = {"Tom", "Jack", "Mary "};
Double[] Salary = {300,400,120 0 };
Try{
Fileoutputstream Fos =
NewFileoutputstream ("Res/salary.txt ");
Dataoutputstream dos =
NewDataoutputstream (FOS );
For(IntI = 0; I <name. length; I ++ ){
Dos. writeUTF (name [I]);
Dos. writeDouble (salary [I]);
}
Dos. close ();
Fos. close ();
///////////////////////
FileInputStream fin =
NewFileInputStream ("res/salary.txt ");
DataInputStream dis =
NewDataInputStream (fin );
For(IntI = 0; I <name. length; I ++ ){
String n = dis. readUTF ();
DoubleS = dis. readDouble ();
System.Out. Println (n + ":" + s );
}
Dis. close ();
Fin. close ();
}Catch(Exception e ){
E. printStackTrace ();
}
}
}
Byte buffer stream
L BufferedInputStream
L BufferedOutputStrem
For the class structure and members, see the documentation.
By default, the stream is not buffered. That is, each read operation requires the operating system to provide one byte. Through bufferedinputstream and
Bufferedoutputstream filters and layers the stream builder to implement buffering.
1. constructor:
Bufferedinputstream (inputstream in)
Bufferedinputstream (inputstream in, intsize) // size: buffer size
2. Code Demonstration:
Bufferedinputstream Bis = newbufferedinputstream (system. In );
Bufferedinputstream Bis = newbufferedinputstream (system. In, 100 );
// Use a buffer byte stream to read and write files
ImportJava. Io .*;
Public classBufferediotest {
Public static voidMain (string [] ARGs ){
Try{
Fileoutputstream Fos =
NewFileoutputstream ("Res/new.txt ");
Bufferedoutputstream Bos =
NewBufferedoutputstream (FOS );
Dataoutputstream dos =
NewDataoutputstream (BOS );
Dos. writeutf ("GoodMorning! ");
Dos. writeint (250 );
Dos. Close ();
Bos. Close ();
FOS. Close ();
Fileinputstream FCM =
NewFileinputstream ("Res/new.txt ");
Bufferedinputstream Bis =
NewBufferedinputstream (FCM );
Datainputstream Dis =
NewDatainputstream (bis );
System.Out. Println (DIS. readutf () + "\ n" + dis. readint ());
Dis. Close ();
Bis. Close ();
FCM. close ();
}Catch(Exception e ){
E. printStackTrace ();
}
}
}