Java实现将从数据库查出的rs数据集写入txt文件中 20
本人是这样写的,方便后期导出到Excel文件,但是现在要求高效率写入,因为这样写入txt文件非常慢,比如要写入100万条数据,需要循环数据列数*100万次,效率很低,有没...
本人是这样写的,方便后期导出到Excel文件,但是现在要求高效率写入,因为这样写入txt文件非常慢,比如要写入100万条数据,需要循环数据列数*100万次,效率很低,有没有什么办法提高效率呢,比如多线程实现怎么实现。希望能给出具体代码,谢谢
rm = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= rm.getColumnCount(); i++) {
if (rm.getColumnDisplaySize(i) > 11 && ("char".equals(rm.getColumnTypeName(i).toLowerCase()) || "varchar".equals(rm.getColumnTypeName(i).toLowerCase()) || "varchar2".equals(rm.getColumnTypeName(i).toLowerCase()))) {
writer.write((byte) 9);// 搞定CSV导出时科学计数法问题
}
String keyStr = rs.getString(i);
String lowerCase = rm.getColumnName(i).toLowerCase();
if (keyStr != null && !"".equals(keyStr) && translate.containsKey(lowerCase)) {
writer.write(filter(trans.getCnnameByOpttypeAndEnname(dictColl, translate.get(lowerCase).toUpperCase(), new String(keyStr.getBytes(), "UTF-8"))).replaceAll("\\<|\\>|\\|", "").getBytes("UTF-8"));
} else {
this.translateStrToNum(translateNum, writer, rs, rm, i);
}
writer.write('|');
}
writer.write('\n');
records++;
if ((++count) == max || count > 5000000) {
break;
}
}
writer.flush(); 展开
rm = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= rm.getColumnCount(); i++) {
if (rm.getColumnDisplaySize(i) > 11 && ("char".equals(rm.getColumnTypeName(i).toLowerCase()) || "varchar".equals(rm.getColumnTypeName(i).toLowerCase()) || "varchar2".equals(rm.getColumnTypeName(i).toLowerCase()))) {
writer.write((byte) 9);// 搞定CSV导出时科学计数法问题
}
String keyStr = rs.getString(i);
String lowerCase = rm.getColumnName(i).toLowerCase();
if (keyStr != null && !"".equals(keyStr) && translate.containsKey(lowerCase)) {
writer.write(filter(trans.getCnnameByOpttypeAndEnname(dictColl, translate.get(lowerCase).toUpperCase(), new String(keyStr.getBytes(), "UTF-8"))).replaceAll("\\<|\\>|\\|", "").getBytes("UTF-8"));
} else {
this.translateStrToNum(translateNum, writer, rs, rm, i);
}
writer.write('|');
}
writer.write('\n');
records++;
if ((++count) == max || count > 5000000) {
break;
}
}
writer.flush(); 展开
1个回答
展开全部
public static void main(String[] args)throws Exception { String path="d:\\aa.txt"; File file=new File(path); FileInputStream fis=new FileInputStream(file); InputStreamReader isr=new InputStreamReader(fis,"utf-8"); BufferedReader br=new BufferedReader(isr); StringBuffer line=new StringBuffer(); String str=null; File file1=new File("d:\\cc.txt"); FileOutputStream fos=new FileOutputStream(file1); OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8"); BufferedWriter bw=new BufferedWriter(osw); while((str=br.readLine())!=null){ line.append(str); line.append("\n"); } System.out.println(line); bw.flush(); bw.close(); } 至于导入数据库,不难吧,创建个连接就可以把从流读出的数据写进去了
追问
我要高效写入
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询