Java 程式,用於從輸入流中讀取下一個位元組的資料


java.io.InputStream.read() 方法用於從輸入流中讀取下一個位元組的資料。此方法不需要任何引數,它以 int 形式返回下一個資料位元組。如果到達流尾,則返回 -1。

演示該方法的程式如下所示 −

示例

import java.io.InputStream;
public class Demo {
   public static void main(String[] args) throws Exception {
      InputStream input = null;
      int i;
      char c;
      try {
         input = new FileInputStream("C://JavaProgram//data.txt");
         System.out.println("The characters in the file are:");
         while((i = input.read())!=-1) {
            c = (char)i;
            System.out.print(c);
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

上述程式的輸出如下 −

輸出

The characters in the file are:
DATA

更新時間: 30-Jul-2019

363 次瀏覽

啟動您的事業

完成課程以獲得認證

開始
廣告