所以我正在研究一个分析特定文件签名的十六进制转储的项目。我遇到的问题是在尝试分析大小为 16+ GB 的大转储时,我收到 OutOfMemoryError: Java heap space 错误。所以我的想法是重新设计我正在使用的算法。
现在我的代码看起来类似于:
public class Test
{
private static ArrayList<String> JPGHeaders = new ArrayList<String>();
private static ArrayList<String> JPGTrailers = new ArrayList<String>();
private static ArrayList<String> entireTextFile = new ArrayList<String>();
public static void main (String[] args)
{
Scanner scanner = new Scanner(new File("C:\\HexAnalyser\\HexDump\\fileTest.txt"));
while (scanner.hasNextLine())
{
entireTextFile.add(scanner.nextLine());
}
for (String line : entireTextFile)
{
if(line.contains(Constants.JPGHEADER))
{
JPGHeaders.add(line);
}
if(line.contains(Constants.JPGTRAILER))
{
JPGTrailers.add(line);
}
}
}
}
所以它将整个文件添加到整个TextFile ArrayList,然后在该ArrayList 中搜索特定的文件头和尾。
对于那些不知道典型的十六进制转储是什么样子的人来说,它类似于:
0012be0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0012bf0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0012c00: ffd8 ffe0 0010 4a46 4946 0001 0201 0050 ......JFIF.....P
0012c10: 0050 0000 ffed 166e 5068 6f74 6f73 686f .P.....nPhotosho
0012c20: 7020 332e 3000 3842 494d 03ed 0000 0000 p 3.0.8BIM......
0012c30: 0010 0050 0000 0001 0001 0050 0000 0001 ...P.......P....
0012c40: 0001 3842 494d 040d 0000 0000 0004 0000 ..8BIM..........
0012c50: 002d 3842 494d 03f3 0000 0000 0008 0000 .-8BIM..........
因为 JPEG 的标题是“ffd8 ffe0”,所以我想要添加到我的 JPGHeaders ArrayList 的唯一行是:
0012c00: ffd8 ffe0 0010 4a46 4946 0001 0201 0050 ......JFIF.....P
我知道这类似于 Linux 中的 grep,但我正在为在 Windows 平台上的 eclipse 中完成的 java 项目执行此操作。有没有更简单的方法可以在最初扫描文件时搜索文件的每一行并将这些特定行添加到相应的数组列表中?或者我是否坚持将整个文件扫描到一个 ArrayList 中,然后在所述 ArrayList 中搜索字符串文字?