“This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

The vast sea of thousands of thousands, thank you for this second you see here. Hope my article is helpful to you!

Wish you in the future, keep love, go to the mountains and seas!

Senior stream I/O

Yesterday we learned about serialization in advanced streams, and in the larger system of IO streams, there are still some advanced streams waiting to be unlocked.

So without further discussion, today we’re going to look at one of these advanced streams — streams that manipulate basic data.

The flow of manipulating basic data

So we’ve been manipulating reference data like String text, but we haven’t come across a stream that can manipulate basic data, right. Let’s talk about it slowly:

DataOutputStream

A data output stream enables an application to write raw Java data types to an output stream in a portable manner. The application can then use the data input stream to read the data.

1. Construction method

  • Public DataOutputStream(OutputStream out) : creates a new DataOutputStream to write data to the specified underlying OutputStream. The argument passed in is of type OutputStream.

    For example, the code is as follows:

    DataOutputStream dos = new DataOutputStream(new FileOutputStream("e:\demo\dos.txt"));       
    Copy the code

2. Write data

As you can see, there are many ways to manipulate data types in the source code:

public class DataStreamDemo { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("e:\demo\dos.txt")); Dos.writebyte (10); dos.writeShort(100); dos.writeInt(1000); dos.writeLong(10000); DOS. WriteFloat (12.34 F); DOS. WriteDouble (12.56); dos.writeChar('a'); dos.writeBoolean(true); Dos.close (); }}Copy the code

And when it’s executed, it’s written to a file, and you can see that the file says, you can’t read it, but it’s not garbled because it’s not stored for us to read, it’s stored for the machine to read. So don’t panic. Let’s read.

DataInputStream

A data input stream allows an application to read raw Java data types from the underlying input stream in a machine-independent manner. The application uses the data output stream to write data that can later be read by the data input stream.

1. Construction method

  • Public DataInputStream(InputStream in) : Creates a DataInputStream that uses the specified InputStream.

    For example, the code is as follows:

    DataInputStream dis = new DataInputStream(new FileInputStream("e:\demo\dos.txt")); 
    Copy the code

2. Read data

As you can see, there are many methods in the source code that can read data types:

public class DataStreamDemo2 { public static void main(String[] args) throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("e:\demo\dos.txt")); // Read data byte b = dis.readbyte (); short s = dis.readShort(); int i = dis.readInt(); long l = dis.readLong(); float f = dis.readFloat(); double d = dis.readDouble(); char c = dis.readChar(); boolean bb = dis.readBoolean(); Dis.close (); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); System.out.println(c); System.out.println(bb); }} 10 100 1000 10000 12.34 12.56 a trueCopy the code

conclusion

I believe that you have a certain understanding of the IO flow in the advanced flow operation of basic data stream class, looking forward to waiting for the next chapter of advanced flow – random access stream teaching!

Of course, there are many streams waiting to watch together next time! Welcome to the next chapter!

So far, the world is closed for today, good night! Although this article is over, BUT I still, never finished. I will try to keep writing articles. The coming days are long, and the horse is slow!

Thank you for seeing this! May you be young and have no regrets!

Note: If there are any mistakes and suggestions, please leave a message! If this article is also helpful to you, I hope you give a lovely and kind attention, thank you very much!