Java 程式從字串/位元組陣列建立流


建立輸入流並設定字串

DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));

getBytes() 方法用於將字串轉換成位元組序列,並返回一個位元組陣列。

現在返回一個單獨的輸入位元組

(char) inputStream.readByte()

例程

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
public class Demo {
public static void main(String[] args) throws Exception {
   DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream("pqrs tu v wxy z".getBytes()));
      System.out.print((char) inputStream.readByte());
      System.out.print((char) inputStream.readByte());
      inputStream.close();
   }
}

輸出

Pq

更新日期: 30-7-2019

483 次瀏覽

開啟您的 職業道路

透過完成課程獲取認證

開始
廣告