在进行文件读取时,文件内数据未读取完成便返回-1结束。求解答(抱拳)
详细代码如下:
//获取二进制文件
private byte[] readLocalFile(String path) throws IOException {
File file = new File(path);
InputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while ((n = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, n);
Log.e("输出内容", "" + (inputStream.read(buffer)));
}
byte[] data = out.toByteArray();
inputStream.close();
return data;
}
log日志如下:
2022-10-14 15:33:04.068 31219-31219/com.example.myapplication E/输出内容: 4096
2022-10-14 15:33:04.069 31219-31219/com.example.myapplication E/输出内容: 4096
2022-10-14 15:33:04.069 31219-31219/com.example.myapplication E/输出内容: -1