📜  Java中的 ObjectInputStream readByte() 方法及示例

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

Java中的 ObjectInputStream readByte() 方法及示例

Java中ObjectInputStream类的readByte()方法用于读取 8 位(字节)。

句法:

public byte readByte()

参数:此方法不接受任何参数。

返回值:此方法返回读取的 8 位字节

错误和异常:该函数抛出三个异常,如下所述:

  • EOFException:如果到达文件末尾,则抛出异常。
  • IOException:如果发生 I/O 错误,则抛出异常。

下面的程序说明了上述方法:

方案一:

Java
// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws IOException
    {
        byte[] array
            = { 1, 34, 23,
                42, 69, 22 };
  
        try {
  
            // create new byte
            // array input stream
            InputStream input
                = new ByteArrayInputStream(array);
  
            // create data input stream
            DataInputStream output
                = new DataInputStream(input);
  
            // readBoolean till the
            // data available to read
            while (output.available() > 0) {
  
                // read one single byte
                byte bt = output.readByte();
  
                // print the byte
                System.out.print(bt + " ");
            }
        }
  
        catch (Exception ex) {
        }
    }
}


Java
// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws IOException
    {
        byte[] array
            = { 'G', 'e', 'e', 'k',
                's', 'f', 'o', 'r',
                'g', 'e', 'e', 'k',
                's' };
  
        try {
  
            // create new byte
            // array input stream
            InputStream input
                = new ByteArrayInputStream(array);
  
            // create data input stream
            DataInputStream output
                = new DataInputStream(input);
  
            // readBoolean till the
            // data available to read
            while (output.available() > 0) {
  
                // read one single byte
                byte bt = output.readByte();
  
                // print the byte
                System.out.print(bt + " ");
            }
            System.out.println();
            System.out.println();
        }
  
        catch (Exception ex) {
        }
    }
}


输出:

方案二:

Java

// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws IOException
    {
        byte[] array
            = { 'G', 'e', 'e', 'k',
                's', 'f', 'o', 'r',
                'g', 'e', 'e', 'k',
                's' };
  
        try {
  
            // create new byte
            // array input stream
            InputStream input
                = new ByteArrayInputStream(array);
  
            // create data input stream
            DataInputStream output
                = new DataInputStream(input);
  
            // readBoolean till the
            // data available to read
            while (output.available() > 0) {
  
                // read one single byte
                byte bt = output.readByte();
  
                // print the byte
                System.out.print(bt + " ");
            }
            System.out.println();
            System.out.println();
        }
  
        catch (Exception ex) {
        }
    }
}

输出:

参考:

https://docs.oracle.com/javase/10/docs/api/java Java()