1. 程式人生 > 實用技巧 >java.io.DateOutputStream:資料專屬位元組輸出流

java.io.DateOutputStream:資料專屬位元組輸出流

java.io.DateOutputStream:資料專屬流。 這個流可以將資料連同資料的型別一併寫入檔案。 注意:這個檔案不是普通文字文件。(這個用記事本打不開) DataOutputStream資料專屬流
package com.javaSe.DateOutputStream;


import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/*
java.io.DateOutputStream:資料專屬流。
這個流可以將資料連同資料的型別一併寫入檔案。
注意:這個檔案不是普通文字文件。(這個用記事本打不開)
*/
public class DataOutputStreamTest01 {
    
public static void main(String[] args) { DataOutputStream ds = null; try { // 建立資料專屬的位元組輸出流 ds = new DataOutputStream(new FileOutputStream("data")); // 寫資料 byte b = 100; short s = 200; int i = 300;
long l = 400L; float f = 3.1F; double d = 3.1415926D; boolean bl = true; char c = '中'; // ds.writeByte(b); // 把資料以及資料的型別一併寫入到檔案當中。 ds.writeShort(s); ds.writeInt(i); ds.writeLong(l); ds.writeFloat(f); ds.writeDouble(d); ds.writeBoolean(bl); ds.writeChar(c); ds.flush(); }
catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ds != null) { try { // 關閉最外層 ds.close(); } catch (IOException e) { e.printStackTrace(); } } } } }