2013-03-05 18 views
2

有什麼像掃描儀的二進制數據?我希望例如使用分隔符,如「\ r \ n」,並在每次調用某個方法時都獲取byte []。你能給我提供可以創造奇蹟的課嗎?類似掃描儀的二進制數據

回答

0

雅加達公共有一個圖書館,你可以使用。只需從jakarta下載.jar並下載它們的.jar,然後通過添加外部jar將其添加到您的構建路徑中。 IOUtils.toByteArray(InputStream input)

+0

但我想要例如整行字節。 – oneat 2013-03-05 18:33:22

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 

希望這有助於

1

考慮使用DataInputStreamDataOutputStream

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();