DataOutputStream(數據輸出流)的認知、源碼和示例
本章介紹DataOutputStream。我們先對DataOutputStream有個大致認識,然後再深入學習它的源碼,最後通過示例加深對它的了解。
DataOutputStream 介紹
DataOutputStream 是數據輸出流。它繼承於FilterOutputStream。
DataOutputStream 是用來裝飾其它輸出流,將DataOutputStream和DataInputStream輸入流配合使用,“允許應用程序以與機器無關方式從底層輸入流中讀寫基本 Java 數據類型”。
DataOutputStream 源碼分析(基於jdk1.7.40)
package java.io; public class DataOutputStream extends FilterOutputStream implements DataOutput { // “數據輸出流”的字節數 protected int written; // “數據輸出流”對應的字節數組 private byte[] bytearr = null; // 構造函數 public DataOutputStream(OutputStream out) { super(out); } // 增加“輸出值” private void incCount(int value) { int temp = written + value; if (temp < 0) { temp = Integer.MAX_VALUE; } written = temp; } // 將int類型的值寫入到“數據輸出流”中 public synchronized void write(int b) throws IOException { out.write(b); incCount(1); } // 將字節數組b從off開始的len個字節,都寫入到“數據輸出流”中 public synchronized void write(byte b[], int off, int len) throws IOException { out.write(b, off, len); incCount(len); } // 清空緩沖,即將緩沖中的數據都寫入到輸出流中 public void flush() throws IOException { out.flush(); } // 將boolean類型的值寫入到“數據輸出流”中 public final void writeBoolean(boolean v) throws IOException { out.write(v ? 1 : 0); incCount(1); } // 將byte類型的值寫入到“數據輸出流”中 public final void writeByte(int v) throws IOException { out.write(v); incCount(1); } // 將short類型的值寫入到“數據輸出流”中 // 注意:short占2個字節 public final void writeShort(int v) throws IOException { // 寫入 short高8位 對應的字節 out.write((v >>> 8) & 0xFF); // 寫入 short低8位 對應的字節 out.write((v >>> 0) & 0xFF); incCount(2); } // 將char類型的值寫入到“數據輸出流”中 // 注意:char占2個字節 public final void writeChar(int v) throws IOException { // 寫入 char高8位 對應的字節 out.write((v >>> 8) & 0xFF); // 寫入 char低8位 對應的字節 out.write((v >>> 0) & 0xFF); incCount(2); } // 將int類型的值寫入到“數據輸出流”中 // 注意:int占4個字節 public final void writeInt(int v) throws IOException { out.write((v >>> 24) & 0xFF); out.write((v >>> 16) & 0xFF); out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(4); } private byte writeBuffer[] = new byte[8]; // 將long類型的值寫入到“數據輸出流”中 // 注意:long占8個字節 public final void writeLong(long v) throws IOException { writeBuffer[0] = (byte)(v >>> 56); writeBuffer[1] = (byte)(v >>> 48); writeBuffer[2] = (byte)(v >>> 40); writeBuffer[3] = (byte)(v >>> 32); writeBuffer[4] = (byte)(v >>> 24); writeBuffer[5] = (byte)(v >>> 16); writeBuffer[6] = (byte)(v >>> 8); writeBuffer[7] = (byte)(v >>> 0); out.write(writeBuffer, 0, 8); incCount(8); } // 將float類型的值寫入到“數據輸出流”中 public final void writeFloat(float v) throws IOException { writeInt(Float.floatToIntBits(v)); } // 將double類型的值寫入到“數據輸出流”中 public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); } // 將String類型的值寫入到“數據輸出流”中 // 實際寫入時,是將String對應的每個字符轉換成byte數據後寫入輸出流中。 public final void writeBytes(String s) throws IOException { int len = s.length(); for (int i = 0 ; i < len ; i++) { out.write((byte)s.charAt(i)); } incCount(len); } // 將String類型的值寫入到“數據輸出流”中 // 實際寫入時,是將String對應的每個字符轉換成char數據後寫入輸出流中。 public final void writeChars(String s) throws IOException { int len = s.length(); for (int i = 0 ; i < len ; i++) { int v = s.charAt(i); out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); } incCount(len * 2); } // 將UTF-8類型的值寫入到“數據輸出流”中 public final void writeUTF(String str) throws IOException { writeUTF(str, this); } // 將String數據以UTF-8類型的形式寫入到“輸出流out”中 static int writeUTF(String str, DataOutput out) throws IOException { //獲取String的長度 int strlen = str.length(); int utflen = 0; int c, count = 0; // 由於UTF-8是1~4個字節不等; // 這裡,根據UTF-8首字節的范圍,判斷UTF-8是幾個字節的。 for (int i = 0; i < strlen; i++) { c = str.charAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { utflen++; } else if (c > 0x07FF) { utflen += 3; } else { utflen += 2; } } if (utflen > 65535) throw new UTFDataFormatException( "encoded string too long: " + utflen + " bytes"); // 新建“字節數組bytearr” // 查看本欄目示例代碼
關於DataOutStream中API的詳細用法,參考示例代碼(DataInputStreamTest.java):
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.lang.SecurityException; /** * DataInputStream 和 DataOutputStream測試程序 * * @author skywang */ public class DataInputStreamTest { private static final int LEN = 5; public static void main(String[] args) { // 測試DataOutputStream,將數據寫入到輸出流中。 testDataOutputStream() ; // 測試DataInputStream,從上面的輸出流結果中讀取數據。 testDataInputStream() ; } /** * DataOutputStream的API測試函數 */ private static void testDataOutputStream() { try { File file = new File("file.txt"); DataOutputStream out = new DataOutputStream( new FileOutputStream(file)); out.writeBoolean(true); out.writeByte((byte)0x41); out.writeChar((char)0x4243); out.writeShort((short)0x4445); out.writeInt(0x12345678); out.writeLong(0x0FEDCBA987654321L); out.writeUTF("abcdefghijklmnopqrstuvwxyz嚴12"); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * DataInputStream的API測試函數 */ private static void testDataInputStream() { try { File file = new File("file.txt"); DataInputStream in = new DataInputStream( new FileInputStream(file)); System.out.printf("byteToHexString(0x8F):0x%s\n", byteToHexString((byte)0x8F)); System.out.printf("charToHexString(0x8FCF):0x%s\n", charToHexString((char)0x8FCF)); System.out.printf("readBoolean():%s\n", in.readBoolean()); System.out.printf("readByte():0x%s\n", byteToHexString(in.readByte())); System.out.printf("readChar():0x%s\n", charToHexString(in.readChar())); System.out.printf("readShort():0x%s\n", shortToHexString(in.readShort())); System.out.printf("readInt():0x%s\n", Integer.toHexString(in.readInt())); System.out.printf("readLong():0x%s\n", Long.toHexString(in.readLong())); System.out.printf("readUTF():%s\n", in.readUTF()); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 打印byte對應的16進制的字符串 private static String byteToHexString(byte val) { return Integer.toHexString(val & 0xff); } // 打印char對應的16進制的字符串 private static String charToHexString(char val) { return Integer.toHexString(val); } // 打印short對應的16進制的字符串 private static String shortToHexString(short val) { return Integer.toHexString(val & 0xffff); } }運行結果:
byteToHexString(0x8F):0x8f
charToHexString(0x8FCF):0x8fcf
readBoolean():true
readByte():0x41
readChar():0x4243
readShort():0x4445
readInt():0x12345678
readLong():0xfedcba987654321
readUTF():abcdefghijklmnopqrstuvwxyz嚴12
來源:http://www.cnblogs.com/skywang12345/p/io_15.html