现在写一个100M的文件大概要4~5分钟
因为经常要写超过100万的数据,所以这速度略显不足,求大神看看,哪里还能优化,多线程的就先别说了,这个就是多线程中的一个线程
* @author liuhongtian * */public class ResultFileTools {
private static Logger log = Logger.getLogger(ResultFileTools.class);
/** * 根据查询结果集生成结果文件内容 * * @param rs * 查询结果集 * @param fileFullName * 结果文件名称(含路径) * @param columnNames * 查询字段名称 * @param properties * 查询属性 * @return 成功或失败 */ public static boolean writeFile(ResultSet rs, String fileFullName, String[] columnNames, String[] propertys) { Handler baseHandler = new BaseHandler(); Handler calcHandler = new CalcHandler(baseHandler); Handler specialHandler = new SpecialHandler(calcHandler);
File file = new File(fileFullName);
// 创建文件 try (BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "gbk"));) { // file.createNewFile();
// 写入文件头 StringBuffer hBuffer = new StringBuffer(); for (String string : columnNames) { hBuffer.append("\"" + string + "\","); } String cols = hBuffer.toString().substring(0, hBuffer.toString().length() - 1); bos.write(cols); bos.newLine(); bos.flush();
// 写入查询结果记录 try { int i = 0 ; while (rs.next()) { StringBuffer buffer = new StringBuffer(); if(i!=0){ buffer.append("\n"); } for (String property : propertys) { String s = specialHandler.toHandle(property, rs); buffer.append("\"" + s + "\","); } bos.write(buffer.toString().substring(0, buffer.toString().length()-1)); buffer = null; i++; } } catch (SQLException e) { log.error("result set process faild!", e); return false; } catch (IOException e) { log.error("record write faild!", e); return false; } } catch (IOException e1) { log.error("open file faild!", e1); return false; }
return true; }
}
添加回答
举报
0/150
提交
取消