Java-io-bufferedoutputstream-write
提供:Dev Guides
Java.io.BufferedOutputStream.Write()メソッド
説明
- java.io.BufferedOutputStream.Write(int)*メソッドは、出力ストリームにバイトを書き込みます。
宣言
以下は、* java.io.BufferedOutputStream.write(int b)*メソッドの宣言です。
public void write(int b)
パラメーター
*b* -出力ストリームに書き込まれるバイト。
戻り値
このメソッドは値を返しません。
例外
*IOException* -I/Oエラーが発生した場合。
例
次の例は、java.io.BufferedOutputStream.write(int b)メソッドの使用方法を示しています。
package com.finddevguides;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws Exception {
ByteArrayOutputStream baos = null;
BufferedOutputStream bos = null;
try {
//create new ByteArrayOutputStream
baos = new ByteArrayOutputStream();
//create new BufferedOutputStream with baos
bos = new BufferedOutputStream(baos);
//assign integer
int b = 87;
//write to stream
bos.write(b);
//force the byte to be written to baos
bos.flush();
//convert ByteArrayOutputStream to bytes
byte[] bytes = baos.toByteArray();
//prints the byte
System.out.println(bytes[0]);
} catch(IOException e) {
//if I/O error occurs.
e.printStackTrace();
} finally {
//releases any system resources associated with the stream
if(baos!=null)
baos.close();
if(bos!=null)
bos.close();
}
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
87