DataInputStreamDataInputStream is the data input stream. It inherits from FilterInputStream.DataInputStream is used to decorate other input streams, which "allows applications to read basic Java data types from the underlying input stream in a machine-independent manner." Applications can use DataOutputStream (data output streams) to write data that is read by the DataInputStream (data input stream).DataInputStrea
Both datainputstream and dataoutputstream are decorative classes for input and output streams in Java, which are easy to use. Today we will discuss the encoding problems encountered when using this class. Zookeeper
Package COM. vincent. example; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexcept
1. Basic Concepts DataOutputStream data output stream allows applications to write Basic Java data types to the output stream in an appropriate way. Then the application can use the data input stream to read data. The DataOutputStream data input stream allows applications to read Basic Java data types from the underlying input stream in a machine-independent manner. Applications can use the data output stream to write data that is later read by the data input stream. Multi-threaded access is not
value as 4-byte value to the output stream before writing high bytes.
void writeUTF (String str);//Writes a string to the basic output stream in a machine-independent way with the UTF-8 revision. The method first writes two bytes with Writeshort to indicate the number of bytes later.
int size ();//Returns the current value of written
DataInputStream allows applications to read basic Java data types from the underlying input stream in a m
ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classDataStream { Public Static voidMain (string[] args)throwsIOException {//Write ("Test5.txt");//Read ("Test5.txt");Readfrombuffer (Writetobuffer ()); } //write to local file Public Static vo
out * Out.write (10000); The Write method can only write one byte * But 10000 is 32 bytes, so it will only write after 8 bits * dataout The Writeint * method of the Putstream Dos object must be written 4 times per write using the Write method of the Out object * Writeint internal must be * Out.write (10000>>24) * Out.write (10000>>16) * Out.write (10000>>8) * Out.write (10000) */ Dos.writeint (+); Dos.writeboolean (TRUE); Dos.writedouble (10.03); Dos.writeutf ("HelloWorld"); Dos.c
Dataoutputstream dos = new dataoutputstream (New bufferedoutputstream (New fileoutputstream ("a.txt"); dos. writeint (5); dos. writeboolean (true); dos. flush (); dos. close (); datainputstream Dis = new datainputstream (New bufferedinputstream (New fileinputstream ("a.txt"); system. out. println (DIS. readint (); system. out. println (DIS. readboolean (); DIS. close (); // learn how to close the stream buf
Usage and precautions of datainputstream and dataoutputstream
Import Java. io. *;
If you directly use datainputstream to read the file content, an exception is thrown:
At this time, even the manual income data cannot be read, and there is no content in the file.
Solution: you must first use dataoutputstream to write data, and then use datainputstream to re
// Remember the data type that is used for output,
// For example, DOS. writeint (100); write, read: DIS. readutf () sometimes has unexpected errors, so you must always remember what data type is used for data type output reading.
// Datainputstream (binary input stream)
Public class datainputstreamdemo {
// Binary stream (read operation)
Public static void main (string [] ARGs ){
Fileinputstream FCM = NULL;
Data
http://www.verejava.com/?id=16994690613914Package Com.io2;import java.io.*;p ublic class testdatainputstream{public static void Main (string[] args) {O Utputstream OS; try {//to the file in int, Double,boolean, String to Data OS = new FileOutputStream ("Res/data . Dat ")); DataOutputStream dos=new dataoutputstream (OS); Dos.writeutf ("Zhang San"); Dos.writeint (20); Dos.writeboolean (TRUE); Dos.writedouble (10.2);
Example program: Packageio;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException; Public classTestdatastream { Public Static voidMain (string[] args) {/*** Create a byte array in memory, Bytearrayoutputstream this pipe is godless to the memory*/Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); //writes the basic Java data type to the output streamDataOutputStream dos =NewDataOutputStream
Java io is really confusing, since there are FileWriter can write the character stream, why create DataOutputStream can also input character flow to the file. The following demo shows that files created by DataOutputStream can be read by FileReader.
programming (because you can easily mix and match attributes) and make the code more complex. The Java I/O class library is so weird because it "must create many classes for an I/O object", which is to add a lot of decorator to a "core" I/O class.
Classes that define the Decorator class interface for InputStream and OutputStream, respectively, are FilterInputStream and Filteroutputstream. These two names are not very good. Both FilterInputStream and Filteroutputstream inherit from the base cla
and OutputStream in the I/O class library respectively. These two classes are necessary conditions for the decorator (so as to provide common interfaces for all objects being modified ).
The FilterInputStream class can accomplish two completely different tasks. DataInputStream allows us to read different basic data types and String objects (all methods start with "read", such as readByte () and readFloat ). With the corresponding
locates the input stream of the pipeline to the datainputstream object so that the program can read data using the readline () method:
Datainputstream inputstream = new datainputstream (zlnpiepe );
After an input stream is created, the program can read data from one row on the screen.
String str = inputstream. readline ();
While (str! = Null)
{
System. out. prin
between the server and the client in order to improve efficiency. Thus, the two classes of DataInputStream and Dataoutstrea are used in read-in and write-up respectively. 2.5 Interface Design
According to the requirements of the topic, the program must enter 4 parameters when running: Source IP, source port, destination IP, destination port. The program starts by judging if 4 parameters have been entered, if no four parameters are entered, or an erro
pipeapp ();
A try block is set because all operations in the program require ioexception exception handling. In try, In order to read data from the source file, the program creates an input stream xfileln for the "input.txt" file ,:
Fileinputstream xfileln = new fileinputstream ("input.txt ");
The new input is passed to the changetoy () method so that the thread ythread can read the file:
Inputstream ylnpipe = pipeapp. changetoy (xfileln );
The changetoy () method creates the thread ythread tha
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.