public void readWord(String uri) {
strmap = new HashMap
map = new HashMap
try {
long start = System.currentTimeMillis();
input = new FileInputStream(getFile(uri));
channel = ((FileInputStream) input).getChannel();
for (int p = 0; p < 5; p++) {
MappedByteBuffer app = channel.map(MapMode.READ_ONLY, p / 5
* file.length(), file.length() * (p + 1) / 5);
byte[] dst = new byte[BUFFER_SIZE];// 每次读出3K的内容
for (int offset = 0; offset < app.capacity(); offset += BUFFER_SIZE) {
if (app.capacity() - offset >= BUFFER_SIZE) {
for (int i = 0; i < BUFFER_SIZE; i++) {
dst[i] = app.get(offset + i);
mapadd(dst[i]);
}
} else {
for (int i = 0; i < app.capacity() - offset; i++) {
dst[i] = app.get(offset + i);
mapadd(dst[i]);
}
}
String str[] = new String(dst,0,dst.length).split("\\W+");//每次出错的位置
//System.out.println(dst.toString());
for (int i = 0; i < str.length; i++) {
if (strmap.containsKey(str[i])) {
Long bg = strmap.get(str[i]);
strmap.put(str[i], ++bg);
} else {
strmap.put(str[i], 1l);
}
}
}
}
long end = System.currentTimeMillis();
System.out.println("**************************************");
System.out.println((end-start)/1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
错误异常代码:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.String.substring(Unknown Source)
at java.lang.String.subSequence(Unknown Source)
at java.util.regex.Pattern.split(Unknown Source)
at java.lang.String.split(Unknown Source)
at java.lang.String.split(Unknown Source)
at Exec9.readWord(Exec9.java:112)
at Exec9.main(Exec9.java:32)
我需要的是一个解决方案!