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
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");
}
}
}