Java-dataoutputstream

提供:Dev Guides
移動先:案内検索

Java-DataOutputStream

DataOutputStreamストリームを使用すると、プリミティブを出力ソースに書き込むことができます。

以下は、DataOutputStreamを作成するコンストラクターです。

DataOutputStream out = DataOutputStream(OutputStream out);

_DataOutputStream_オブジェクトを手に入れると、ヘルパーメソッドのリストが表示されます。このメソッドを使用して、ストリームを記述したり、ストリームに対して他の操作を実行したりできます。

Sr.No. Method & Description
1

public final void write(byte[] w, int off, int len)throws IOException

指定されたバイト配列からポイントoffで始まるlenバイトを基になるストリームに書き込みます。

2

Public final int write(byte [] b)throws IOException

このデータ出力ストリームに書き込まれた現在のバイト数を書き込みます。 バッファに書き込まれた合計バイト数を返します。

3

(a) public final void writeBooolean()throws IOException,

(b)public final void writeByte()はIOExceptionをスローします

(c)public final void writeShort()はIOExceptionをスローします

  • (d)public final void writeInt()はIOException *をスローします

これらのメソッドは、特定のプリミティブ型のデータをバイトとして出力ストリームに書き込みます。

4

Public void flush()throws IOException

データ出力ストリームをフラッシュします。

5

public final void writeBytes(String s) throws IOException

文字列を一連のバイトとして基礎となる出力ストリームに書き込みます。 ストリング内の各文字は、その上位8ビットを破棄することにより、順番に書き出されます。

以下は、DataInputStreamとDataOutputStreamを示す例です。 この例では、ファイルtest.txtで指定された5行を読み取り、それらの行を大文字に変換し、最後に別のファイルtest1.txtにコピーします。

import java.io.*;
public class DataInput_Stream {

   public static void main(String args[])throws IOException {

     //writing string to a file encoded as modified UTF-8
      DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("E:\\file.txt"));
      dataOut.writeUTF("hello");

     //Reading data from the same file
      DataInputStream dataIn = new DataInputStream(new FileInputStream("E:\\file.txt"));

      while(dataIn.available()>0) {
         String k = dataIn.readUTF();
         System.out.print(k+" ");
      }
   }
}

ここに上記のプログラムのサンプル実行があります-

出力

THIS IS TEST 1  ,
THIS IS TEST 2  ,
THIS IS TEST 3  ,
THIS IS TEST 4  ,
THIS IS TEST 5  ,