将二进制文件读入字节数组Java

问题描述 投票:-1回答:1

我需要读取一个二进制文件,并将每个字节保存到字节数组中。我已经阅读了其他关于stackoverflow的文章,但无法弄清楚为什么我的作品不起作用。这是我所拥有的:

String fileOne = "file1.bin";
byte[] byteArray = new byte[1000];
try{
    FileInputStream fileIS = new FileInputStream(fileOne);
    ObjectInputStream is = new ObjectInputStream(fileIS);
    is.read(byteArray);
    is.close();
    for(int i =0; i < byteArray.length; i++){
        System.out.println(byteArray[i]);
    }
    }
catch (FileNotFoundException e){
    e.toString();
    System.exit(0);
}
catch (IOException io){
    io.toString();
    System.exit(0);
}
java arrays binary binaryfiles readfile
1个回答
0
投票

更新:除了删除ObjectInputStream并通过FileInputStream执行命令以外,我一切都保持不变。现在它正在打印二进制文件中的字节,并在末尾打印一吨空白吗?由于.read()读取整个文件,因此如何删除空白?

© www.soinside.com 2019 - 2024. All rights reserved.