java.lang.outofmemoryerror java heap space
现在有一个很大的文件,大概有100w条记录,但是是按确定的格式,格式如下,现在想用java把记录填进数据库,数据库有两个字段,GID和GSEQ,分别存储>100000000和下面的字母。
>100000000
AATGGGGCCCGGAAGDDD
DDDGGGGGGGGGGGGGGG
DDDDDDDDDD
>100000001
...........
java代码如下:
try
{
BufferedReader fin = new BufferedReader(new FileReader("metaphlan_markers.fna"));
//id=name=null;
StringBuffer temp,strbuf;
String str = null;
boolean flag = false;
//Pattern p = Pattern.compile("(\\d{9})\\s(\\D*)");
try
{
str = fin.readLine();
while(str!=null)
{
//System.out.println(str);
strbuf = new StringBuffer(str);
//Matcher m = p.matcher(strbuf);
temp = new StringBuffer();
if(strbuf.charAt(0)=='>')
{
if(temp!=null)
{
//System.out.println(temp);
try{
String iquery = "insert into geneid_geneseq(GeneId,GeneSeq) values(?,?)";
pstmt = conn.prepareStatement(iquery);
pstmt.setString(1,strbuf.toString());
pstmt.setString(2,temp.toString());
pstmt.executeUpdate();
}catch(
SQLException e){
System.out.println("3");
e.printStackTrace();
}
}
strbuf.deleteCharAt(0);
//System.out.println(strbuf);
}
else
{
temp.append(str);
}
//strbuf = new StringBuffer(fin.readLine());
str = fin.readLine();
//System.out.println(st);
//System.out.println("st");
}
}catch(Exception e){
String err = e.toString();
System.out.println(err);
}
}catch(
IOException e){
System.out.println(e);
}
每次程序都会报java heap space的错误,后来在运行程序时使用java a -Xms512m -Xmx512m后运行到50w条数据时就结束了,求解答。
------解决方案--------------------缓冲啊!!
File file = new File("metaphlan_markers.fna");
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件
String line = "";
while((line = reader.readLine()) != null){
//TODO: write your business
}