當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.DataInputStream.readByte()用法及代碼示例


描述

這個java.io.DataInputStream.readByte()方法讀取並返回一個輸入字節。該字節是 -128 到 127 範圍內的有符號值。

聲明

以下是聲明java.io.DataInputStream.readByte()方法 -

public final byte readByte()

參數

NA

返回值

讀取的字節值。

異常

  • IOException− 如果流已關閉,或發生任何 I/O 錯誤。

  • EOFException− 如果輸入流已到達末尾。

示例

下麵的例子展示了 java.io.DataInputStream.readByte() 方法的用法。

package com.tutorialspoint;

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

相關用法


注:本文由純淨天空篩選整理自 Java.io.DataInputStream.readByte() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。