Java-io-datainputstream-readbyte

提供:Dev Guides
移動先:案内検索

Java.io.DataInputStream.readByte()メソッド

説明

  • java.io.DataInputStream.readByte()*メソッドは、1つの入力バイトを読み取って返します。 バイトは、-128〜127の範囲の符号付き値です。

宣言

以下は* java.io.DataInputStream.readByte()*メソッドの宣言です-

public final byte readByte()

パラメーター

NA

戻り値

読み取られたバイト値。

例外

  • IOException -ストリームが閉じられている場合、またはI/Oエラーが発生した場合。
  • EOFException -入力ストリームが最後に達した場合。

次の例は、java.io.DataInputStream.readByte()メソッドの使用法を示しています。

package com.finddevguides;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {65, 0, 0, 68, 69};

      try {
        //create new byte array input stream
         is = new ByteArrayInputStream(buf);

        //create data input stream
         dis = new DataInputStream(is);

        //readBoolean till the data available to read
         while( dis.available() >0) {

           //read one single byte
            byte b = dis.readByte();

           //print the byte
            System.out.print(b+" ");
         }

      } catch(Exception e) {
        //if any I/O error occurs
         e.printStackTrace();
      } finally {
        //releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

65 0 0 68 69