📜  Java Java类设置 1

📅  最后修改于: 2022-05-13 01:54:42.415000             🧑  作者: Mango

Java Java类设置 1

数据输入流使应用程序能够以与机器无关的方式(而不是原始字节)从底层输入流中读取原始Java数据类型。这就是它被称为 DataInputStream 的原因——因为它读取数据(数字)而不仅仅是字节。

应用程序使用数据输出流写入数据,这些数据稍后可以由数据输入流读取。数据输入流和数据输出流以对 UTF-8 稍作修改的格式表示 Unicode字符串。 DataInputStream 对于多线程访问不一定是安全的。线程安全是可选的,并且是此类中方法的用户的责任。

首先让我们讨论一下这个类的构造函数

ConstructorAction Performed
DataInputStream(InputStream in)Creates a DataInputStream that uses the specified underlying InputStream.

现在让我们讨论这个类的方法,这些方法在下面以表格格式描述,如下所示:

Methods Action performed 
read(byte[] b)Reads some number of bytes from the contained input stream and stores them into the buffer array b.
read(byte[] b, int off, int len)Reads up to length bytes of data from the contained input stream into an array of bytes. 
readBoolean()Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. 
readChar()Reads two input bytes and returns a char value. 
readUTF()Reads data from the underlying input stream and converts the bytes into a Unicode string.
readByte()Read one input byte and returns a byte value.
readFloat()Read four input bytes and returns a float value.
readFully()Read bytes equal to the length of the byte array
readDouble()Reads eight input bytes and returns a double value.
readInt()Reads four input bytes and returns an int value. 
readLine()Reading lines of text
readLong()Reading eight input bytes and returns a long value
readShort()Read two input bytes and return a short value.
readUnsignedByte()Read byte and return as an integer
readUnsignedShort()Read two input bytes and returns as an integer array
skipBytes()Skips over n bytes of data from input stream

执行:

现在让我们来实现上面已经讨论过的这个类的一些上述方法

示例 1

Java
// Java program to Demonstrate DataInputStream Class
 
// Importing I/O classes
import java.io.*;
 
// Main class
class DataInputStreamDemo {
 
    // Main driver method
    public static void main(String args[]) throws IOException {
 
        // Writing the data
 
        // Try block to check for exceptions
        try ( DataOutputStream dout =
                        new DataOutputStream(new FileOutputStream("file.dat")) ) {
 
            dout.writeDouble(1.1);
            dout.writeInt(55);
            dout.writeBoolean(true);
            dout.writeChar('4');
        }
 
        // Catch block to handle the exceptions
        catch (FileNotFoundException ex) {
 
            // Display message when FileNotFoundException occurs
            System.out.println("Cannot Open the Output File");
            return;
        }
 
        // Reading the data back.
 
        // Try block to check for exceptions
        try ( DataInputStream din =
                        new DataInputStream(new FileInputStream("file.dat")) ) {
 
            // Illustrating readDouble() method
            double a = din.readDouble();
 
            // Illustrating readInt() method
            int b = din.readInt();
 
            // Illustrating readBoolean() method
            boolean c = din.readBoolean();
 
            // Illustrating readChar() method
            char d = din.readChar();
 
            // Print the values
            System.out.println("Values: " + a + " " + b + " " + c + " " + d);
        }
 
        // Catch block to handle the exceptions
        catch (FileNotFoundException e) {
 
            // Display message when FileNotFoundException occurs
            System.out.println("Cannot Open the Input File");
            return;
        }
    }
}


输出: