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


Java Java.io.DataOutputStream用法及代碼示例

數據輸出流允許應用程序以可移植的方式將原始 Java 數據類型寫入輸出流。然後,應用程序可以使用數據輸入流讀回數據。在繼續討論此類的方法之前,讓我們先討論一下此類的構造函數。

構造函數:DataOutputStream(OutputStream 輸出)

創建新的數據輸出流以將數據寫入指定的底層輸出流。現在讓我們在實現此類之前先討論該類的重要方法。

方法一:無效flush()

刷新此數據輸出流。

句法:

public void flush() throws IOException

Note: It do overrides the flush() method in class FilterOutputStream

拋出異常:IO異常

方法二:size()

返回寫入的計數器的當前值,到目前為止寫入此數據輸出流的字節數

用法:

public final int size()

返回類型:寫入字段的整數值

方法三:寫()

將指定字節數組中從偏移量 off 開始的 len 個字節寫入底層輸出流。

用法:

void write (byte[] b, int off, int len)

參數:

  • 字節數組
  • int 關閉
  • 字節數組的長度

返回類型:空白

方法四:

用法

public void write(byte[] b, int off,int len)

Note: It overrides write() method in FilterOutputStream class.

參數:

  • 數據
  • 數據中的起始偏移量
  • 要寫入的字節數

異常:IO異常

方法5:void write(int b):

將指定字節(參數 b 的低八位)寫入基礎輸出流。

用法:

public void write(int b)
throws IOException

返回類型:空白

參數:要寫入的字節

方法六:writeBoolean()

將布爾值作為 1 字節值寫入底層輸出流。

用法:

void writeBoolean (boolean v) 

返回類型:空白

參數:布爾值

方法七:writeBoolean()

用法:

public final void writeBoolean(boolean v)
throws IOException 

參數:要寫入的布爾值。

拋出:IOException

方法8:writeByte()

將一個字節作為 1 字節值寫入底層輸出流。

用法:

public final void writeByte(int v)
throws IOException 

參數:要寫入的字節值

Exception Thrown:IO異常

方法九:writeChar()

將 char 作為 2 字節值寫入底層輸出流,先高字節。

用法:

public final void writeChar(int v)
throws IOException

參數:要寫入的字符值。

Exception: IO異常

方法10:void writeDouble(double v)

使用 Double 類中的 doubleToLongBits 方法將 double 參數轉換為 long,然後將該 long 值作為 8 字節數量(高字節在前)寫入基礎輸出流。

用法:

public final void writeDouble(double v)
throws IOException

參數:要寫入的雙精度值

Exception thrown:IO異常

方法十一: writeFloat (浮點 v)

使用 Float 類中的 floatToIntBits 方法將 float 參數轉換為 int,然後將該 int 值以 4 字節數量(最高有效位在前)的形式寫入基礎輸出流。

用法:

public final void writeFloat(float v)
throws IOException

返回類型:空白

參數:要寫入的浮點值。

Exception thrown:IO異常

方法十二:writeInt()

將 int 作為四個字節寫入底層輸出流,先是高字節。

用法:

public final void writeInt(int v)
throws IOException

Parameters: 要寫入的int。

返回類型:空白

拋出異常:將 int 作為四個字節寫入底層輸出流,先是高字節。

方法13:writeLong()

將 long 作為八個字節寫入底層輸出流,先是高字節。

用法:

public final void writeLong(long v)
throws IOException

參數:很長要寫。

拋出:IOException

方法14:writeShort():

將一個短值作為兩個字節寫入底層輸出流,先是高字節。

返回類型:空白

參數:待寫的短篇

Syntax :

public final void writeShort(int v)
throws IOException

方法15:writeUTF(字符串str)

使用修改後的 UTF-8 編碼以 machine-independent 方式將字符串寫入基礎輸出流。

參數:要寫入的字符串

返回類型:空白

拋出異常:IO異常

Note: There are certain important points to be remembered as listed: 

  • DataOutputStream and DataInputStream are often used together.
  • When a DataOutputStream is closed (by calling close( )), the underlying stream specified by out is also closed automatically.
  • There is no longer any explicit close() method call. The try-with-resources construct takes care of that.

例子:

Java


// Java Program to Demonstrate DataOutputStream 
  
// Importing required classes 
import java.io.*; 
  
// Main class 
// DataOutputStreamDemo 
class GFG { 
        // Main driver method 
        public static void main(String args[]) throws IOException { 
                // Try block to check for exceptions 
                // Writing the data using DataOutputStream 
                try ( DataOutputStream dout = 
                                                new DataOutputStream(new FileOutputStream("file.dat")) ) { 
                        dout.writeDouble(1.1); 
                        dout.writeInt(55); 
                        dout.writeBoolean(true); 
                        dout.writeChar('4'); 
                } 
  
                catch (FileNotFoundException ex) { 
                        System.out.println("Cannot Open the Output File"); 
                        return; 
                } 
  
                // Reading the data back using DataInputStream 
                try ( DataInputStream din = 
                                                new DataInputStream(new FileInputStream("file.dat")) ) { 
                        double a = din.readDouble(); 
                        int b = din.readInt(); 
  
                        boolean c = din.readBoolean(); 
                        char d = din.readChar(); 
  
                        System.out.println("Values: " + a + " " + b + " " + c + " " + d); 
                } 
  
                // Catch block to handle FileNotFoundException 
                catch (FileNotFoundException e) { 
                        System.out.println("Cannot Open the Input File"); 
                        return; 
                } 
        } 
}

輸出:

Values: 1.1 55 true 4

Note: The above program uses try-with-resources so do it require JDK 7 or later.



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.DataOutputStream in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。