java 中带有大文本文件的子字符串

Substring with large text file in java

我有一个文本文件(253 MB 大小),我写了这段代码:

    String content = new Scanner(new File ("C:\Users\user1\IdeaProjects\untitled\file")).useDelimiter("\Z").next();

    System.out.println(content.substring(19,26)); 

但我收到此错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

at java.nio.HeapCharBuffer.<init>(HeapCharBuffer.java:57)
at java.nio.CharBuffer.allocate(CharBuffer.java:335)
at java.util.Scanner.makeSpace(Scanner.java:840)
at java.util.Scanner.readInput(Scanner.java:795)
at java.util.Scanner.next(Scanner.java:1369)

要在大文件中使用子字符串函数,我该怎么办... 请帮助我...

如果您只需要一个 7 个字母的子字符串,那么读取整个文件似乎非常无效。这实际上取决于你的文件结构,但如果你确定你需要的只是来自位置 19..26 的字节,那么你可以简单地使用 RandomAccessFile 读取这些字节,例如:

RandomAccessFile raf = new RandomAccessFile(new File("..."), "r"); // "r" means 'open the file for reading'
raf.seek(19L); // or other position depending on your file structure
byte[] b = new byte[7]; // choose how big the byte-buffer should be - how long is the substring
raf.read(b); // read from file into the buffer
System.out.println(new String(b)); // create a String from the byte-buffer

当然,您在创建字符串时应该选择合适的字符集编码,具体取决于您的输入文件,例如new String(b, "UTF-8")