在Java中将二进制文件读入ArrayList [英] Reading a binary file into ArrayList in Java
问题描述
StringBuilder sb = new StringBuilder();
String fileName =/path/10.bin;
byte [] buffer;
try {
buffer = Files.readAllBytes(Paths.get(fileName));
(byte b:buffer){
sb.append(String.format(%02X,b));
}
System.out.println(sb.toString());
$ b $ catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
$ b $ p
$ b $是我的问题的真正的方式还是我必须使用Arraylist缓冲?如果我使用单字节数组进行缓冲,是否必须清除其他二进制文件的缓冲区。
编辑:153单位表示153文件(1.bin,2。 bin ... 153.bin)
解决方案我假定一个单元是一个字节。这是正确的吗?
ArrayList不适合字节缓冲。它是一个实现了List接口的数组的包装类(通过这个接口,大部分的权力都被定义了)。 http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList。如果你只是想从文件中读取字节,你可以使用FileInputStream。
p> http://docs.oracle.com/javase/tutorial/essential /io/bytestreams.html
以下是一个简单的例子,它从包含hello world!的文件中读取输入。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
$ b $ public class Input {
public static void main(String [] args)throws FileNotFoundException,IOException {
try(FileInputStream in = new FileInputStream(test.txt)) {
int c; ((c = in.read())!= -1)
System.out.print((byte)c +);
以字节为单位的输出:104 101 108 108 111 32 119 111 114 108 100 33 13 10
I want to read binary file in java. I have 153(1.bin, 2.bin...153.bin) bin file. I must read that. I thought that I must use ArrayList for buffering. But I could not do that. How can I do this ?
After some research, I found that way in this title(Reading a binary input stream into a single byte array in Java). There is code like below in this title.
StringBuilder sb = new StringBuilder();
String fileName = "/path/10.bin";
byte[] buffer;
try {
buffer = Files.readAllBytes(Paths.get(fileName));
for(byte b : buffer){
sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Is it true way for my question or do I must use Arraylist for buffering ? If I use single byte array for buffering, do I must clear the buffer for the other binary files.
Edit : 153 unit means 153 file(1.bin,2.bin ... 153.bin)
解决方案 I'm assuming a unit is one byte. Is this correct?
An ArrayList is not appropriate for byte buffering. It is a wrapper class for an array that implements the List interface (by which most of it's power is defined.)
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
If you simply want to read bytes in from a file, you could use FileInputStream.
http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html
Here's a simple example that reads input from a file containing "hello world!"
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Input {
public static void main(String[] args) throws FileNotFoundException, IOException {
try (FileInputStream in = new FileInputStream("test.txt")) {
int c;
while ( (c = in.read()) != -1 )
System.out.print((byte)c + " ");
}
}
}
Output in bytes: 104 101 108 108 111 32 119 111 114 108 100 33 13 10
这篇关于在Java中将二进制文件读入ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!