出力ストリームをバイト配列へ変換するには
いつもお世話になります。
BufferedOutpuStreamで取得したオブジェクトを(ByteArrayOutputStreamでインスタンス生成)、バイト配列へ変換するにはどのようにすればよいでしょうか。
ByteArrayOutputStreamだと、toByteArrayメソッドでバイト配列へ変換できるのですが、効率化を考慮し、BufferedOutputStreamへ出力するように
しているのですが、この場合だと取得したBufferedOutputStreamをバイト配列へ変換する方法が分からず困っています。
宜しくお願いします。
No.1ベストアンサー
- 回答日時:
>ByteArrayOutputStreamでインスタンス生成
ByteBufferじゃ駄目なんですか?
Bruce Eckel, "Thinking in Java (4th Edition)" (Prentice Hall, 2006)
によると、p.946~p.948に、次のようにあります。
The Java “new” I/O library, introduced in JDK 1.4 in the java.nio.* packages, has one goal: speed.
(中略)
The speed comes from using structures that are closer to the operating system’s way of performing I/O: channels and buffers.
(中略)
The only kind of buffer that communicates directly with a channel is a ByteBuffer?that is, a buffer that holds raw bytes.
(中略)
Three of the classes in the “old” I/O have been modified so that they produce a FileChannel: FileInputStream, FileOutputStream, and, for both reading and writing, RandomAccessFile.
Notice that these are the byte manipulation streams, in keeping with the low-level nature of nio.
The Reader and Writer character-mode classes do not produce channels, but the class java.nio.channels.Channels has utility methods to produce Readers and Writers from channels.
Here’s a simple example that exercises all three types of stream to produce channels that are writeable, read/writeable, and readable:
//: io/GetChannel.java
// Getting channels from streams
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
public class GetChannel {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
// Write a file:
FileChannel fc =
new FileOutputStream("data.txt").getChannel();
fc.write(ByteBuffer.wrap("Some text ".getBytes()));
fc.close();
// Add to the end of the file:
fc =
new RandomAccessFile("data.txt", "rw").getChannel();
fc.position(fc.size()); // Move to the end
fc.write(ByteBuffer.wrap("Some more".getBytes()));
fc.close();
// Read the file:
fc = new FileInputStream("data.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
fc.read(buff);
buff.flip();
while(buff.hasRemaining())
System.out.print((char)buff.get());
}
} /* Output:
Some text Some more
*///:~
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# C言語初心者 ポインタについて、お助けください、、 2 2023/03/15 23:50
- Excel(エクセル) 配列操作について 5 2023/04/18 07:27
- その他(プログラミング・Web制作) プログラミング python pandas 固定長データの出力 2 2022/08/16 11:22
- PHP 配列の値の更新方法について 1 2022/08/05 09:49
- マウス・キーボード 英語配列のキーボードには変換キーなどありませんが日本語配列のように使うにはどうしたらいいんですか?ど 2 2023/01/20 03:18
- アンテナ・ケーブル 家屋テレビ用アンテナ配線 4 2023/04/27 15:00
- その他(コンピューター・テクノロジー) 【Tableau Desktop】文字列から8桁の数字を日付型(yyyyMMdd)として取得 1 2023/07/31 10:17
- Visual Basic(VBA) 入力ボックスが繰り返しポップアップして止まらない。 下記コードでファイル名の変更をしたいのですが、変 1 2022/09/08 11:27
- 生物学 アミノ酸配列頑張っは分かってるのですがmRNAがまだ分からないのですが、アミノ酸配列からmRNAに変 1 2023/06/28 15:41
- 生物学 シャトルベクターの大腸菌への形質転換 1 2022/07/02 23:19
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
『args[]』とは?
-
[Ljava.lang.Stringってなんですか
-
javaについて
-
n番目に大きな値を探索する
-
この警告はどうすれば?
-
javaでEUC-JP文字列→UTF-8への変換
-
配列の中に複数存在する数がい...
-
配列について
-
配列で、値の入っている要素数...
-
配列をセッションに割り当てた後で
-
【C#】ハッシュテーブル(連想...
-
セッションを使ったint型の値の...
-
main(String[] args) の args...
-
java
-
Javaで文字と数字が混ざったも...
-
無名配列の作成方法について質問
-
多次元配列はなぜ必要???
-
2次元の配列となっているクラ...
-
JSPでデータ一覧表示
-
C++で、Excelの内容を取得する...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
[Ljava.lang.Stringってなんですか
-
この警告はどうすれば?
-
C#で動的配列Listの中身をListB...
-
javaで大容量テキストファイル...
-
C言語でunsigned char配列を連...
-
n番目に大きな値を探索する
-
JavaのBase64デコードで正しく...
-
配列で、値の入っている要素数...
-
Javaで文字を漢字であるか否か...
-
配列の中に複数存在する数がい...
-
データベースから取得したデー...
-
【C#】ハッシュテーブル(連想...
-
Javaで文字と数字が混ざったも...
-
cloneメソッドについて (java)
-
ArrayListからBean配列に値をセ...
-
配列をセッションに割り当てた後で
-
int配列をbyte配列に変換
-
セッションを使ったint型の値の...
-
javamailで複数人にメールを送...
-
【Ajax通信&Java】配列の受け取...
おすすめ情報