Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java DataInputStream Class · Private Archive

Java DataInputStream Class · Private Archive

Java Stream Java Stream


Java DataInputStream Class

A data input stream allows an application to read primitive Java data types from an underlying input stream in a machine-independent manner.


DataInputStream dis = new DataInputStream(InputStream in);

Another way to create it is to receive a byte array and two integer variables off and len, where off indicates the first byte to read and len indicates the length of bytes to read.

No. Method Description
1 public final int read(byte[] r, int off, int len)throws IOException
The following constructor is used to create a data input stream object. len Reads
2 Public final int read(byte [] b)throws IOException
bytes into a byte array from the contained input stream. If len is -1, the number of bytes read is returned. b it.
3
  1. public final Boolean readBooolean()throws IOException,
  2. public final byte readByte()throws IOException,
  3. public final short readShort()throws IOException
  4. public final Int readInt()throws IOException
  5. Reads a certain number of bytes from the contained input stream and stores them in the buffer array

    Example

    Reads bytes from the input stream, returning the two bytes from the input stream as the corresponding primitive data type return value. | | 4 | **public String readLine() throws IOException** Reads the next line of text from the input stream. |

    The following example demonstrates the use of DataInputStream and DataOutputStream; it reads 5 lines from the text file test.txt, converts them to uppercase letters, and finally saves them in another file test1.txt.

    runoob1
    runoob2
    runoob3
    runoob4
    runoob5
    

    Example

    import

    java

    .

    io

    .*;

    public

    class

    Test

    {

    public

    static

    void

    main

    (

    String

    args

    [

    ]

    )

    throws

    IOException

    {

    DataInputStream

    in

    =

    new

    DataInputStream

    (

    new

    FileInputStream

    (

    "

    test.txt

    "

    )

    )

    ;

    DataOutputStream

    out

    =

    new

    DataOutputStream

    (

    new

    FileOutputStream

    (

    "

    test1.txt

    "

    )

    )

    ;

    BufferedReader

    d

    =

    new

    BufferedReader

    (

    new

    InputStreamReader

    (

    in

    )

    )

    ;

    String

    count

    ;

    while

    (

    (

    count

    =

    d

    .

    readLine

    (

    )

    )

    !=

    null

    )

    {

    String

    u

    =

    count

    .

    toUpperCase

    (

    )

    ;

    System

    .

    out

    .

    println

    (

    u

    )

    ;

    out

    .

    writeBytes

    (

    u

    "

    ,

    "

    )

    ;

    }

    d

    .

    close

    (

    )

    ;

    out

    .

    close

    (

    )

    ;

    }

    }

    The compilation and execution results of the above example are as follows:

    
    RUNOOB1
    RUNOOB2
    RUNOOB3
    RUNOOB4
    RUNOOB5
    

    Java Stream Java Stream