バイナリデータ用のスキャナーのようなものはありますか? たとえば、「\r\n」のような区切り文字を使用して、メソッドを呼び出すたびに byte[] を取得したいと思います。そのような奇跡を起こすことができるクラスを私に提供できますか?
1817 次
3 に答える
1
と の使用を検討しDataInputStream
てDataOutputStream
ください。
File file = new File("binaryFile.dat");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
byte[] array1 = ...
byte[] array2 = ...
byte[] array3 = ...
dos.writeInt(array1.length);
for(byte b : array1) dos.wrtieByte(b);
dos.writeInt(array2.length);
for(byte b : array2) dos.wrtieByte(b);
dos.writeInt(array3.length);
for(byte b : array3) dos.wrtieByte(b);
そしてそれを次のように読んでください:
File file = new File("binaryFile.dat");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
byte[] array1 = new byte[dis.readInt()];
for(int i = 0; i < array1.length; i++) array1[i] = dis.readByte();
byte[] array2 = new byte[dis.readInt()];
for(int i = 0; i < array2.length; i++) array2[i] = dis.readByte();
byte[] array3 = new byte[dis.readInt()];
for(int i = 0; i < array3.length; i++) array3[i] = dis.readByte();
于 2013-03-05T19:07:26.393 に答える
0
Jakarta commons には、使用できるライブラリがあります。Jakarta から .jarをダウンロードし、その .jar をダウンロードして、それを add external jars 経由でビルド パスに追加するだけです。IOUtils.toByteArray(InputStream input)
于 2013-03-05T18:32:13.397 に答える
0
java.io.InputStreamクラスを使用できます。
InputStream is = new FileInputStream("your_file");
byte[] b = new byte[is.available()]; //reads how much bytes are readable from file
is.read(b);//reads the file and save all read bytes into b
お役に立てれば
于 2013-03-05T18:34:56.450 に答える