Java Fundamentals-Data flow

Source: Internet
Author: User

DataInputStream class and DataOutputStream class

in the previous study, we know that data flow processing data refers to a byte or byte array, but in fact, it is often not the case, it requires the data stream can directly read, write a variety of Java language basic data types. For example, in a file to hold 300 integer values, read from, you want to press int for the basic unit to read, this can greatly save time. You need to use DataInputStream and Dataoutputstream,datainputstream and DataOutputStream not only to support methods in the parent class InputStream and OutputStream, Read and write data in the original way, while also allowing the data stream to read and write the basic data types of the Java language, including int, float, double, and double. Its constructor form is as follows:
DataInputStream (OBJ)

DataOutputStream (OBJ)

OBJ is a well-established input-output data stream object
DataInputStream and DataOutputStream are able to manipulate these primitive types because they have a specific set of methods.

The common methods of DataInputStream are as follows:
-byte ReadByte ()

long Readlong ()

double readdouble ()

, Boolean Readboolean ()

String readUTF ()

int readInt ()

< Span style= "font-size:14pt" >       -> float readfloat ()

< Span style= "font-size:14pt" > & Nbsp;    -> short Readshort ()

< Span style= "font-size:14pt" > Span style= "font-size:14pt" >     -> char ReadChar ()
      These methods read the types of int, byte, float, char, and so on, respectively.
     -> void writebyte (int a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" >      -> void Writelong (Long a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" >      -> void wirteDouble (Double A)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" >       -> void Writeboolean (Boolean a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > & nbsp;    -> void writeUTF (String a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > Span style= "font-size:14pt" >     -> void writeint (int a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" >      -> void WriteChar (char A)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" >       -> void Writefloat (float a)

< Span style= "font-size:14pt" > < Span style= "font-size:14pt" > < Span style= "font-size:14pt" >      -> void WriteShort ( Short a)
      These methods write to the type int, byte, float , char, and so on, respectively.

The following are examples of DataInputStream and DataOutputStream:

Package com.feicuiedu.test;

Import java.io.*;
public class liu_test{

public static void Main (String args[]) throws exception{

FileOutputStream fos = new FileOutputStream ("Text.txt");

DataOutputStream dos = new DataOutputStream (FOS);

Dos.writeint (12);

Dos.writedouble (2.23d);

              Dos.writeBoolean (true);

               Dos.writeutf ("Java Data Flow test #");

fos.close ();

FileInputStream fis = new FileInputStream ("Text.txt");

datainputstream dis = new DataInputStream (FIS);

System.out.println (Dis.readint ());  

System.out.println (Dis.readdouble ());

System.out.println (Dis.readboolean ());  

System.out.println (Dis.readutf ());

Fis.close ();

           }     }

Output Result:

Java Fundamentals-Data flow

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.