DataOutputStream類writeBytes()方法
- writeBytes() 方法可在
java.io
包。 - writeBytes() 方法用於將給定的字符串作為字節序列寫入基本輸出流。
- writeBytes() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- writeBytes() 方法可能在寫入字符串時拋出異常。
IOException:在獲取任何輸入/輸出錯誤時可能會拋出此異常。
用法:
public final void writeBytes(String str);
參數:
String str
– 表示要寫入基本數據輸出流的字符串。
返回值:
該方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of void writeBytes(String str) method
// of DataOutputStream
import java.io.*;
public class WriteBytesOfDOS {
public static void main(String[] args) throws Exception {
ByteArrayOutputStream baos_stm = null;
DataOutputStream dos_stm = null;
String str = "Java World";
try {
// Instantiates ByteArrayOutputStream, DataOutputStream
baos_stm = new ByteArrayOutputStream();
dos_stm = new DataOutputStream(baos_stm);
// By using writeBytes() method isto write
// value in bytes from the given str
// to the dos_stm stream
dos_stm.writeBytes(str);
// By using flush() method isto
// flush output bytes are written out
// to the basic output stream
dos_stm.flush();
// Display str in bytes
System.out.println("dos_stm.writeBytes(str):" + str);
System.out.println();
// By using toByArray() method isto
// convert the stream baos_stm to byte
// array
byte[] by = baos_stm.toByteArray();
// Loop to display each byte to the
// baos_stm data till its end
for (byte val:by) {
// Display corresponding byte to each of the
// given character represented in a string
System.out.println("dos_stm.writeByte():" + val);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// this block is to free all necessary system
// resources linked with the stream
if (baos_stm != null)
baos_stm.close();
if (dos_stm != null)
dos_stm.close();
}
}
}
輸出
dos_stm.writeBytes(str):Java World dos_stm.writeByte():74 dos_stm.writeByte():97 dos_stm.writeByte():118 dos_stm.writeByte():97 dos_stm.writeByte():32 dos_stm.writeByte():87 dos_stm.writeByte():111 dos_stm.writeByte():114 dos_stm.writeByte():108 dos_stm.writeByte():100
相關用法
- Java DataOutputStream writeByte()用法及代碼示例
- Java DataOutputStream writeBoolean()用法及代碼示例
- Java DataOutputStream writeFloat()用法及代碼示例
- Java DataOutputStream writeInt()用法及代碼示例
- Java DataOutputStream writeShort()用法及代碼示例
- Java DataOutputStream writeChars()用法及代碼示例
- Java DataOutputStream writeUTF()用法及代碼示例
- Java DataOutputStream writeLong()用法及代碼示例
- Java DataOutputStream writeDouble()用法及代碼示例
- Java DataOutputStream writeChar()用法及代碼示例
- Java DataOutputStream flush()用法及代碼示例
- Java DataOutputStream size()用法及代碼示例
- Java DataInputStream skipBytes()用法及代碼示例
- Java DataInputStream readUnsignedShort()用法及代碼示例
- Java DataInputStream readDouble()用法及代碼示例
- Java DataInputStream read()用法及代碼示例
- Java DataInputStream readInt()用法及代碼示例
- Java DataInputStream readLong()用法及代碼示例
- Java DataInputStream readShort()用法及代碼示例
- Java DataInputStream readUnsignedByte()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java DataOutputStream writeBytes() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。