Java - DataOutputStream writeByte(int value)



描述

Java DataInputStream writeBytes(String s) 方法將字串作為位元組序列寫入底層輸出流。字串中的每個字元都按順序寫入,方法是丟棄其高八位。如果未丟擲異常,則寫入計數器將增加 s 的長度。

宣告

以下是 java.io.DataOutputStream.writeBytes(String s) 方法的宣告:

public final void writeBytes(String s)

引數

s − 作為位元組寫入流的字串源。

返回值

此方法不返回值。

異常

IOException − 如果發生 I/O 錯誤。

示例 1

以下示例演示了 Java DataOutputStream writeBytes(String b) 方法的使用。我們建立了 ByteArrayOutputStream 和 DataOutputStream 引用。一個字串初始化了一些文字。建立了一個 ByteArrayOutputStream 物件。然後用之前建立的 ByteArrayOutputStream 物件初始化 DataOutputStream。使用 writeBytes() 方法將字串寫入流。下一步,使用 flush() 方法重新整理流,並迭代 ByteArrayOutputStream 以列印寫入流的位元組。最後,我們關閉所有流。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      DataOutputStream dos = null;
      String s = "Hello World!!";      
      try {
         
         // create byte array output stream
         baos = new ByteArrayOutputStream();
         
         // create data output stream
         dos = new DataOutputStream(baos);
         
         // write to the output stream from the string
         dos.writeBytes(s);
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
         
            // print value
            System.out.print(b + " ");
         }         
      } catch(Exception e) {
         
         // if any error occurs
         e.printStackTrace();
      } finally {
         
         // releases all system resources from the streams
         if(baos!=null)
            baos.close();
         if(dos!=null)
            dos.close();
      }
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

72 101 108 108 111 32 87 111 114 108 100 33 33  

示例 2

以下示例演示了 Java DataOutputStream writeBytes(String b) 方法的使用。我們建立了 ByteArrayOutputStream 和 DataOutputStream 引用。建立了一個 ByteArrayOutputStream 物件。然後用之前建立的 ByteArrayOutputStream 物件初始化 DataOutputStream。作為一個特例,我們關閉流,然後將字串寫入流以檢查它是否允許寫入已關閉的流。下一步,使用 flush() 方法重新整理流,並迭代 ByteArrayOutputStream 以將寫入流的位元組列印為字元。最後,我們關閉所有流。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      DataOutputStream dos = null;           
      try {
         
         // create byte array output stream
         baos = new ByteArrayOutputStream();
         
         // create data output stream
         dos = new DataOutputStream(baos);
         
         // close the stream
         dos.close();
		 
         // write to the output stream from the string
         dos.writeBytes("Hello World!!");
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
            
            // print value
            System.out.print(b + " ");
         }
         
      } catch(Exception e) {
         
         // if any error occurs
         e.printStackTrace();
      } finally {
         
         // releases all system resources from the streams
         if(baos!=null)
            baos.close();
         if(dos!=null)
            dos.close();
      }
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

72 101 108 108 111 32 87 111 114 108 100 33 33 
java_dataoutputstream.htm
廣告
© . All rights reserved.