当前位置: 代码迷 >> J2EE >> 关于文件读取写入等性能有关问题
  详细解决方案

关于文件读取写入等性能有关问题

热度:328   发布时间:2016-04-22 03:21:53.0
关于文件读取写入等性能问题
==========================================
fname="d:\\bankmessagedemo\\"+fname+".txt";
  BufferedWriter out = new BufferedWriter(new FileWriter(fname));

  /*把业务记录写入文件中*/
  IndivInfomod indivinfomod=new IndivInfomod();
  if (type.equals("1")) {
  back = indivinfomod.oneWriteData(out, strDate); //自动生成
  }
  else {
  back = indivinfomod.oneWriteData(out, sdate); //手动生成
  }
=========================================
public int oneWriteData(BufferedWriter out, String sdate) {
  System.out.println("dingting---------Start:");
  odbcConnection mySybase = new odbcConnection();
  Connection conndb = null;
  Statement stmt = null;
  ResultSet rs=null;
  int back = 0; //如果返回0为不成功,如果返回1为成功。
  try {
  conndb = mySybase.getConnection();
  String readStr = "select RFDT,IOMK,CYNO,BRNO,AMCD,OTCD,ACIT,TSAM,PRID,USID,MARK from XKJ where RFDT='" +sdate + "' order by BRNO";

  stmt = conndb.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  rs = stmt.executeQuery(readStr);
  System.out.println("dingting:Now begin to excute from bankcredit..........");

  StringBuffer sb = new StringBuffer(512);
  while (rs.next()) {
  sb.append("|");
  sb.append(rs.getString(1)).append("|");
  sb.append(rs.getString(2)).append("|");
  sb.append(rs.getString(3)).append("|");
  sb.append(rs.getString(4)).append("|");
  sb.append(rs.getString(5)).append("|");
  sb.append(rs.getString(6)).append("|");
  sb.append(rs.getString(7)).append("|");
  sb.append(rs.getString(8)).append("|");
  sb.append(rs.getString(9)).append("|");
  sb.append(rs.getString(9)).append("|");
  sb.append(rs.getString(10)).append("|");
  sb.append(rs.getString(11)).append("|");
  sb.append("\n");
  }
  if (!sb.equals("")) {
  back = 1;
  }
  System.out.println("dingting:Now end to excute from bankcredit..........");
  out.write(sb.toString());
  out.flush();
  out.close();
  }
  catch (Exception ex) {
  System.out.println(mySybase.getErrorMessage(ex,"Application error,please contact to your Software Designer!").toString());
  }
  finally {
  try {
  if(conndb != null){
  conndb.close();
  rs.close();
  }
  }
  catch (Exception e) {
  }
  }
  return back;
  }
===================================
操作:查询数据库,读取数据,插入到文本文件中,但是查询数据量达到100万时,报内存溢出,请大家看看程序都有什么问题
报错信息:Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space  


------解决方案--------------------
百万数据非同小可,这样做溢出是在所难免的 

建议分批write
------解决方案--------------------
做个类似批量处理的处理
Java code
        while (rs.next()) {               sb.append("|");               sb.append(rs.getString(1)).append("|");               sb.append(rs.getString(2)).append("|");               sb.append(rs.getString(3)).append("|");               sb.append(rs.getString(4)).append("|");               sb.append(rs.getString(5)).append("|");               sb.append(rs.getString(6)).append("|");               sb.append(rs.getString(7)).append("|");               sb.append(rs.getString(8)).append("|");               sb.append(rs.getString(9)).append("|");               sb.append(rs.getString(9)).append("|");               sb.append(rs.getString(10)).append("|");               sb.append(rs.getString(11)).append("|");               sb.append("\n");               count ++;              if (count % 10000 == 0) {                  out.write(sb.toString());                   sb.delete(0, sb.length());              }            }
  相关解决方案