「Java」ByteArrayInputStreamとByteArrayOutputStreamクラスのサンプル
説明
ByteArrayOutputStream
データがバイト配列に書き込まれる出力ストリームを実装します。
ByteArrayInputStream
ストリームから読み込まれたバイトを格納する内部バッファーを保持しています。
使用例
package com.arkgame.study.javlesson;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteStreamTestDemo {
public static void main(String[] args) {
ByteArrayOutputStream baOs = new ByteArrayOutputStream(120);
while (baOs.size() != 16) {
// 入力
try {
baOs.write(System.in.read());
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
byte b[] = baOs.toByteArray();
System.out.println("Print the content");
for (int x = 0; x < b.length; x++) {
// 文字列を出力
System.out.print((char) b[x] + "");
}
System.out.println(" ");
int c;
ByteArrayInputStream baIS = new ByteArrayInputStream(b);
System.out.println("Converting characters to Upper case ");
for (int y = 0; y < 1; y++) {
while ((c = baIS.read()) != -1) {
System.out.println(Character.toUpperCase((char) c));
}
baIS.reset();
}
}
}
実行結果
study in arkgame
Print the content
study in arkgame
Converting characters to Upper case
S
T
U
D
Y
I
N
A
R
K
G
A
M
E