我有一个包含100万行文本的大型json文件。
我使用这种方法
String filePath = Thread.currentThread().getContextClassLoader()
.getResource("text.json").getPath();
fs=new FileInputStream(new File(filePath));
BufferedReader br;
br = new BufferedReader(new InputStreamReader(fs),10 * 1024 * 1024);
String line = "";
Map<String, Object> resultMap = null;
StringBuilder sb=new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
但错误:堆空间蚀
我尝试修改VM参数-Xms
也不能使用
还有其他方法吗?
由于
答案 0 :(得分:-1)
尝试使用RandomAccessFile ::
正如@rossum建议的那样,在块中读取文件,执行操作并清除缓冲区数据。
public static String getInputString(String filepath) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile
(filepath, "r");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024); //Reading the file in chunk of 1024 bytes[1KB]
StringBuilder sb = new StringBuilder("");
while(channel.read(buffer) > 0)
{
buffer.flip(); //Ready for get ,put operation
for (int i = 0; i < buffer.limit(); i++)
{
sb.append((char)buffer.get()); //Reading every character and appending to Stringbuilder
}
buffer.clear(); //After performing opeartion clear the buffer
}
channel.close();
randomAccessFile.close();
return sb.toString();
}