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


Java ObjectInputStream readChar()用法及代碼示例


ObjectInputStream 類的 readChar() 方法用於讀取 16 位(2 字節)字符。

用法

public char readChar() throws IOException

參數

沒有傳遞參數。

返回

讀取的 16 位字符。

拋出

EOFException - 如果到達文件末尾。

IOException - 如果發生其他 I/O 錯誤。

例子1

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ObjectInputStreamreadCharsExample1  {
   public static void main(String[] args) throws IOException {
      
      byte[] buf = {'j', 'a', 'v', 'a', 'T', 'p', 'o', 'i', 'n', 't'};
      
      try {
         // create file output stream
       FileOutputStream   fileoutstream = new FileOutputStream("file1.txt");
         
         // create data output stream
        DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
         
         // for each byte in the buffer
         for (byte b:buf) {
            // write character to the dos
            dataoutstream.writeChar(b);
         }
         
         // force bytes to the underlying stream
         dataoutstream.flush();
         
         // create file input stream
         InputStream inpstream = new FileInputStream("file1.txt");
         
         // create new data input stream
        DataInputStream datainpstream = new DataInputStream(inpstream);
         
         // read till end of the stream
         while(datainpstream.available()>0) {
         
            // read character
            char c = datainpstream.readChar();
            
            // print
            System.out.print("    "+c);
         }
         
      } catch(Exception e) {
            
         e.printStackTrace();
      } 
   }
}

輸出:

j    a    v    a    T    p    o    i    n    t

例子2

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ObjectInputStreamreadCharsExample2  {
   public static void main(String[] args) throws IOException {
      
      byte[] carray = {'j', 'a', 'v', 'a', 'T', 'p', 'o', 'i', 'n', 't'};
      
      try {
         // create file output stream
       FileOutputStream   fileoutstream = new FileOutputStream("file1.txt");
         
         // create data output stream
        DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
         
         // for each byte in the buffer
         for (byte b:carray) {
            // write character to the dos
            dataoutstream.writeChar(b);
         }
         
         // force bytes to the underlying stream
         dataoutstream.flush();
         
         // create file input stream
         InputStream inpstream = new FileInputStream("file1.txt");
         
         // create new data input stream
        DataInputStream datainpstream = new DataInputStream(inpstream);
         
         // read till end of the stream
         for(int i=0;i<=carray.length;i++){ 
         
            // read character
            char c = datainpstream.readChar();
            
            // print
            System.out.print("    "+c);
         }
         
      } catch(Exception e) {
        
      } 
   }
}

輸出:

j    a    v    a    T    p    o    i    n    t




相關用法


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