简体   繁体   English

读取大文件错误“ outofmemoryerror”(java)

[英]Read large file error “outofmemoryerror”(java)

sorry for my english. 对不起我的英语不好。 I want to read a large file, but when I read error occurs outOfMemoryError . 我想读取一个大文件,但是当我读取错误时发生outOfMemoryError I do not understand how to work with memory in the application. 我不明白如何在应用程序中使用内存。 The following code does not work: 以下代码不起作用:

try {

    StringBuilder fileData = new StringBuilder(1000);
    BufferedReader reader = new BufferedReader(new FileReader(file));

    char[] buf = new char[8192];
    int bytesread = 0, 
        bytesBuffered = 0;

    while( (bytesread = reader.read( buf )) > -1 ) {

        String readData = String.valueOf(buf, 0, bytesread);
        bytesBuffered += bytesread;

        fileData.append(readData); //this is error

        if (bytesBuffered > 1024 * 1024) { 
            bytesBuffered = 0;
        }
    }

    System.out.println(fileData.toString().toCharArray());
} finally {

}

Try this. 尝试这个。 This might be helpful :- 这可能会有所帮助:-

try{
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String txt = "";
    while( (txt = reader.read()) != null){
        System.out.println(txt);
    }
}catch(Exception e){
   System.out.println("Error : "+e.getMessage());
}

You need pre allocate a large buffer to avoid reallocate. 您需要预先分配一个大缓冲区以避免重新分配。

File file = ...;
StringBuilder fileData = new StringBuilder(file.size());

And running with large heap size: 并以大堆大小运行:

java -Xmx2G

==== update ====更新

A while loop using buffer doesn't need too memory to run. 使用缓冲区的while循环不需要太多内存即可运行。 Treat input like a stream, match your search string with the stream. 将输入视为流,将搜索字符串与流匹配。 It's a really simple state machine. 这是一个非常简单的状态机。 If you need search multiple words, you can find a TrieTree implementation(support stream) for that. 如果您需要搜索多个单词,则可以为此找到一个TrieTree实现(支持流)。

// the match state model
...xxxxxxabxxxxxaxxxxxabcdexxxx...
         ab     a     abcd

    File file = new File("path_to_your_file");
    String yourSearchWord = "abcd";
    int matchIndex = 0;
    boolean matchPrefix = false;
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        int chr;
        while ((chr = reader.read()) != -1) {
            if (matchPrefix == false) {
                char searchChar = yourSearchWord.charAt(0);
                if (chr == searchChar) {
                    matchPrefix = true;
                    matchIndex = 0;
                }
            } else {
                char searchChar = yourSearchWord.charAt(++matchIndex);
                if (chr == searchChar) {
                    if (matchIndex == yourSearchWord.length() - 1) {
                        // match!!
                        System.out.println("match: " + matchIndex);
                        matchPrefix = false;
                        matchIndex = 0;
                    }
                } else {
                    matchPrefix = false;
                    matchIndex = 0;
                }
            }
        }
    }

You should not hold such big files in memory, because you run out of it, as you see. 如您所见,您不应该在内存中保存如此大的文件,因为它用完了。 Since you use Java 7, you need to read the file manually as stream and check the content on the fly. 由于您使用的是Java 7,因此您需要以流形式手动读取文件并即时检查内容。 Otherwise you could use the stream API of Java 8. This is just an example. 否则,您可以使用Java 8的流API。这只是一个示例。 It works, but keep in mind, that the position of the found word could vary due to encoding issues, so this is no production code: 可以,但是请记住,由于编码问题,找到的单词的位置可能会有所不同,因此这不是生产代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileReader
{
    private static String wordToFind = "SEARCHED_WORD";
    private static File file = new File("YOUR_FILE");
    private static int currentMatchingPosition;
    private static int foundAtPosition = -1;
    private static int charsRead;

    public static void main(String[] args) throws IOException
    {
        try (FileInputStream fis = new FileInputStream(file))
        {
            System.out.println("Total size to read (in bytes) : " + fis.available());

            int c;
            while ((c = fis.read()) != -1)
            {
                charsRead++;
                checkContent(c);
            }

            if (foundAtPosition > -1)
            {
                System.out.println("Found word at position: " + (foundAtPosition - wordToFind.length()));
            }
            else
            {
                System.out.println("Didnt't find the word!");
            }

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void checkContent(int c)
    {
        if (currentMatchingPosition >= wordToFind.length())
        {
            //already found....
            return;
        }

        if (wordToFind.charAt(currentMatchingPosition) == (char)c)
        {
            foundAtPosition = charsRead;
            currentMatchingPosition++;
        }
        else
        {
            currentMatchingPosition = 0;
            foundAtPosition = -1;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OutOfMemoryError:尝试读取大文件时的Java堆空间 - OutOfMemoryError: Java heap space when trying to read large file 在Java中对大型文件执行“文件到字节[]”时发生OutOfMemoryError错误? - OutOfMemoryError error while doing “File to byte[]” in Java for a large file? 读取大文本文件时出现 Java OutOfMemoryError - Java OutOfMemoryError in reading a large text file 加载大量文件时出现Java ImageIO.read()OutOfMemoryError - Java ImageIO.read() OutOfMemoryError while loading large numbers of files “ OutOfMemoryError:超出了GC开销限制”:使用java解析大型json文件 - “OutOfMemoryError: GC overhead limit exceeded”: parse large json file with java java.lang.OutOfMemoryError 处理大型 CSV 文件时 - java.lang.OutOfMemoryError While processing a Large CSV file 合并分块文件中的大文件部分时出现Java OutOfMemoryError - Java OutOfMemoryError while merge large file parts from chunked files 在Android中加密大文件时出现java.lang.OutOfMemoryError - java.lang.OutOfMemoryError on encrytping a large file in android Java-使用扫描仪在Delimeter上拆分大型SQL文本文件(OutOfMemoryError) - Java - Splitting Large SQL Text File on Delimeter Using Scanner (OutOfMemoryError) Java - 使用 Apache POI 写入大型 Excel 文件时出现 OutOfMemoryError - Java - OutOfMemoryError when writing large Excel file with Apache POI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM