CharBuffer位于内存映射的ByteBuffer之上,无需使用大量堆空间 [英] CharBuffer on top of a memory-mapped ByteBuffer without using lots of heap space

查看:174
本文介绍了CharBuffer位于内存映射的ByteBuffer之上,无需使用大量堆空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java代码来搜索大型txt文件(6-8Gb)中的电子邮件地址和密码.我已经编写了代码,它可以处理200Mb的txt文件并给出了输出.但是,当我输入500Mb文件时,它将显示以下错误.

I am writing a java code to search of email address and passwords in a large txt file (6-8Gb). I have written the code and it worked with 200Mb txt file and given the output. But when i input a 500Mb file it displays the following error.

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:331)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:792)
at regular.expression_fyp.RegularExpression_fyp.main(RegularExpression_fyp.java:56)
Java Result: 1

我是Java编程的新手,所以我需要您的任何帮助来解决此问题.我该怎么做才能解决这个问题?请给我任何建议,我也附上了我的代码.谢谢.

I am new to java programming so i need any help from you to solve this problem. What should i do to solve this problem? Please send me any suggessions and i have attached my code as well. Thank you.

import java.io.FileInputStream;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpression_fyp
{

   public static void main(String[] argv) throws Exception {

        String pattern = "\\w[%A-Za-z0-9-]+\\%40\\w+\\.com\\w[%A-Za-z0-9]+";
        Pattern r = Pattern.compile(pattern);

        FileInputStream input = new FileInputStream("E:\\test7.txt");
        FileChannel channel = input.getChannel();

        ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
        CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);

        Matcher matcher = r.matcher(cbuf);

        if (matcher.find( )) {
            System.out.println("Found value: " + matcher.group(0) );

        } else {
            System.out.println("NO MATCH");
        }
    }
}

推荐答案

感谢大家的g跃贡献!由于我使用的是netbeans,所以我发现了另一种方法(今天).根据我对项目属性的了解,在运行下,我向vm选项添加了-Xmx1000m.所以现在我的程序运行正常.但是我想知道这是否会导致我在futuer中出现任何错误,因为我想使此程序可执行.因此,它也应该在其他Windows操作系统中运行.这个变化会对我的未来造成任何问题吗?

Thank you everyone for your grate contributions! Since i am using netbeans i have found another way (today). according to that i wen to the project properties and under run,i have added -Xmx1000m to the vm options. So now my programe works fine. But i want to know whether this can cause me any error in the futuer because i am suppose to make this programe executable. so this should run in other windows OS as well. Will this change make any problem for me in the futuer?

这篇关于CharBuffer位于内存映射的ByteBuffer之上,无需使用大量堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆