所以我一直在尝试制作一个小程序,将文件输入字节数组,然后将该字节数组转换为十六进制,然后转换为二进制。然后它将使用二进制值(我还没有想到到达这个阶段时要做什么),然后将其保存为自定义文件。
我研究了很多互联网代码,我可以将文件转换为字节数组和十六进制,但问题是我无法将大文件转换为字节数组(内存不足)。
这是不是完全失败的代码
public void rundis(Path pp) {
byte bb[] = null;
try {
bb = Files.readAllBytes(pp); //Files.toByteArray(pathhold);
System.out.println("byte array made");
} catch (Exception e) {
e.printStackTrace();
}
if (bb.length != 0 || bb != null) {
System.out.println("byte array filled");
//send to method to turn into hex
} else {
System.out.println("byte array NOT filled");
}
}
我知道这个过程应该如何进行,但我不知道如何正确编码。
如果您有兴趣,流程如下:
- 输入文件使用
File
- 将文件的块读取到字节数组中。前任。每个字节数组记录保存 600 字节
- 发送该块以将其转换为十六进制值 -->
Integer.tohexstring
- 发送该十六进制值块以将其转换为二进制值 -->
Integer.toBinarystring
- 搞乱二进制值
- 逐行保存到自定义文件
Problem:: 我不知道如何将一个巨大的文件逐块转换为字节数组进行处理。
任何和所有帮助将不胜感激,感谢您的阅读:)
要对输入进行分块,请使用 FileInputStream:
Path pp = FileSystems.getDefault().getPath("logs", "access.log");
final int BUFFER_SIZE = 1024*1024; //this is actually bytes
FileInputStream fis = new FileInputStream(pp.toFile());
byte[] buffer = new byte[BUFFER_SIZE];
int read = 0;
while( ( read = fis.read( buffer ) ) > 0 ){
// call your other methodes here...
}
fis.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)