子串与java中的大文本文件

时间:2017-03-29 17:02:05

标签: java substring large-files

我有一个文本文件(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)

在大文件中使用substring函数,我该怎么办... 请帮帮我......

1 个答案:

答案 0 :(得分:0)

如果你只需要一个7个字母的子字符串,那么读取整个文件似乎非常无效。这实际上取决于您的文件结构,但如果您确定所需的只是位置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")

当然,您应该在创建String时选择适当的字符集编码,具体取决于您的输入文件,例如: uWSGI