bufferedwriter写文件 什么时候写文件

帐号:密码:下次自动登录{url:/nForum/slist.json?uid=guest&root=list-section}{url:/nForum/nlist.json?uid=guest&root=list-section}
贴数:4&分页:Wind发信人: edcrfv (Wind), 信区: JavaTutorial
标&&题: 请教 BufferedWriter 按编码格式输出 问题
发信站: 水木社区 (Thu Oct 29 10:40:29 2009), 站内 && 【 以下文字转载自 Java 讨论区 】
发信人: edcrfv (Wind), 信区: Java
标&&题: 请教 BufferedWriter 按编码格式输出 问题
发信站: 水木社区 (Thu Oct 29 10:38:02 2009), 站内 && 用Java程序写FTP上传文件到主机.
读到输入是分开两半的"|"(EBCDIC下,"6A"对应字符),结果变成了 "B&".&& 输出时设定格式为"UTF-8",如下:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(data.getOutputStream(),"UTF-8"));
out.write(line, 0, line.length())
其中&&data是FTPDataSocket对象, &&&&&& 试过写到文本文件里,同样设定"UTF-8",如下,结果是正确的.
FileOutputStream writerStream = new FileOutputStream("e:\\WCTTest\\DST\\a.txt");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( &&&&&&&&&&&&&&&&&&&&&& writerStream, "UTF-8"));
writer.write(line + "\r\n"); &&&& 请教牛人,这两个差在哪,跟主机的某些配置有关吗?&&先谢谢了.
Shrek&&N&&Donkey &&&&&& DLKSHBDNZDSHH,TZHXSHYJX! &&&& ※ 修改:·edcrfv 于 Oct 29 10:39:53 2009 修改本文·[FROM: 202.108.130.*]
※ 来源:·水木社区 ·[FROM: 202.108.130.*]
无聊了发信人: hero080 (无聊了), 信区: JavaTutorial
标&&题: Re: 请教 BufferedWriter 按编码格式输出 问题 (转载)
发信站: 水木社区 (Thu Oct 29 11:23:01 2009), 站内 && 我觉得重点不在这些代码,重点在服务器端的代码。我不太熟FTP协议,但我觉得你用OutputWriter写出去的数据那边并没有用对应的InputReader读,所以会乱码。你得查清楚服务器的Reader是怎么decode的。
【 在 edcrfv (Wind) 的大作中提到: 】
: 【 以下文字转载自 Java 讨论区 】
: 发信人: edcrfv (Wind), 信区: Java
: 标&&题: 请教 BufferedWriter 按编码格式输出 问题
: ...................
挥舞着圣光织就的剑 &&&&&&&& 我冲向命运的彼岸 && ※ 来源:·BBS 水木清华站 smth.org·[FROM: 219.244.164.*] &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 99.59.244.*]
Wind发信人: edcrfv (Wind), 信区: JavaTutorial
标&&题: Re: 请教 BufferedWriter 按编码格式输出 问题 (转载)
发信站: 水木社区 (Thu Oct 29 14:44:26 2009), 站内 && 非常感谢, 我也不了解FTP, 请问怎么查服务器的Reader怎么decode.
我的远端是连到大机上. && 【 在 hero080 (无聊了) 的大作中提到: 】
: 我觉得重点不在这些代码,重点在服务器端的代码。我不太熟FTP协议,但我觉得你用OutputWriter写出去的数据那边并没有用对应的InputReader读,所以会乱码。你得查清楚服务器的Reader是怎么decode的。
Shrek&&N&&Donkey &&&&&& DLKSHBDNZDSHH,TZHXSHYJX! &&&& ※ 来源:·水木社区 ·[FROM: 202.108.130.*]
无聊了发信人: hero080 (无聊了), 信区: JavaTutorial
标&&题: Re: 请教 BufferedWriter 按编码格式输出 问题 (转载)
发信站: 水木社区 (Thu Oct 29 15:13:53 2009), 站内 && 这个,我觉得,直接查一般性的FTP协议就可以了。
【 在 edcrfv (Wind) 的大作中提到: 】
: 非常感谢, 我也不了解FTP, 请问怎么查服务器的Reader怎么decode.
: 我的远端是连到大机上.
pkuschool&&&& 打TVT &&&&&&和&&&&
TheReaver&&&& 打PVP &&&&&&&&&& 和&&&&
Heidern&&&&&& 打ZVZ &&&&&&&&&&&&&&&&和&&&&
daizhiwei&&&& 打RVR
※ 来源:·BBS 水木清华站 smth.org·[FROM: 219.244.164.*]&&&&&& ※ 来源:·水木社区 newsmth.net·[FROM: 99.59.244.*]
文章数:4&分页:用户名:棉花糖TWO
文章数:28
访问量:65205
注册日期:
阅读量:1297
阅读量:3317
阅读量:459882
阅读量:1144315
51CTO推荐博文
1.创建Student类存储每个学生信息,属性(学号,姓名,出生日期,得分)
2.从c:/test/student.txt文件中读取学生信息。如下:
&&&&&& 学号,姓名,出生日期,得分
&&&&&& 1,张三,,80
&&&&&&&2,李四,,40
&&&&&& 3,王五,,60
&&&&&& 4,赵六,,70
&&&&&& 5,小明,,70
&&&&&& 6,李大嘴,,70
3.使用List存储6名学生的信息。
4.使用集合排序,将学生信息按时得分从高到低排序,得分相同时按照出生日期升序排序。
5.输出排序后的结果到c:/test/result.txt文件中,输出格式与输入格式相同,第一行是表头。
6.在代码中使用泛型,读文本文件使用BufferedReader,写文本文件使用BufferedWriter
1.创建StudentInfo类,实现Comparable接口
package&com. &&import&java.text.ParseE &import&java.text.SimpleDateF &import&java.util.D &&public&class&StudentInfo&implements&Comparable&StudentInfo& &{ &&&&&private&String&&&&&&&&&&&&&&&&private&String&&&&&&&&&&&&&&private&String&&&&&&&&&&private&String&&&&&&&&&&&&&&public&String&getId() &&&&&{ &&&&&&&&&return& &&&&&} &&&&&&public&void&setId(String&id) &&&&&{ &&&&&&&&&this.id&=& &&&&&} &&&&&&public&String&getName() &&&&&{ &&&&&&&&&return& &&&&&} &&&&&&public&void&setName(String&name) &&&&&{ &&&&&&&&&this.name&=& &&&&&} &&&&&&public&String&getBirthday() &&&&&{ &&&&&&&&&return& &&&&&} &&&&&&public&void&setBirthday(String&birthday) &&&&&{ &&&&&&&&&this.birthday&=& &&&&&} &&&&&&public&String&getScore() &&&&&{ &&&&&&&&&return& &&&&&} &&&&&&public&void&setScore(String&score) &&&&&{ &&&&&&&&&this.score&=& &&&&&} &&&&&&&&&&&&&&&&&&public&int&compareTo(StudentInfo&objStu) &&&&&{ &&&&&&&&&SimpleDateFormat&dateFormat&=&new&SimpleDateFormat(&yyyy-MM-dd&); &&&&&&&&&&&&&&&&&&if&(this.score.equals(objStu.getScore())) &&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&&&&&&Date&date1&=&null; &&&&&&&&&&&&&try&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&date1&=&dateFormat.parse(this.birthday); &&&&&&&&&&&&&} &&&&&&&&&&&&&catch&(ParseException&e1) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&e1.printStackTrace(); &&&&&&&&&&&&&} &&&&&&&&&&&&&Date&date2&=&null; &&&&&&&&&&&&&try&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&date2&=&dateFormat.parse(objStu.getBirthday()); &&&&&&&&&&&&&} &&&&&&&&&&&&&catch&(ParseException&e2) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&e2.printStackTrace(); &&&&&&&&&&&&&} &&&&&&&&&&&&&&&&&&&&&&&&&&return&pareTo(date2); &&&&&&&&&} &&&&&&&&&&&&&&&&&&return&objStu.getScore().compareTo(this.score); &&&&&} &} &
2.读写文件类StudentFile.java
package&com. &&import&java.io.BufferedR &import&java.io.BufferedW &import&java.io.FileNotFoundE &import&java.io.FileR &import&java.io.FileW &import&java.io.IOE &import&java.util.ArrayL &import&java.util.L &&public&class&StudentFile &{ &&&&&&&&&&&&&&&&&&public&List&StudentInfo&&readFile(String&path)&throws&Exception &&&&&{ &&&&&&&&&List&StudentInfo&&studentList&=&new&ArrayList&StudentInfo&(); &&&&&&&&&BufferedReader&br&=&null; &&&&&&&&&try&&&&&&&&&{ &&&&&&&&&&&&&br&=&new&BufferedReader(new&FileReader(path)); &&&&&&&&&&&&&String&line&=&null; &&&&&&&&&&&&&StudentInfo&student&=&null; &&&&&&&&&&&&&while&((line&=&br.readLine())&!=&null) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&student&=&new&StudentInfo(); &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&String[]&studentStr&=&line.split(&,&); &&&&&&&&&&&&&&&&&student.setId(studentStr[0]); &&&&&&&&&&&&&&&&&student.setName(studentStr[1]); &&&&&&&&&&&&&&&&&student.setBirthday(studentStr[2]); &&&&&&&&&&&&&&&&&student.setScore(studentStr[3]); &&&&&&&&&&&&&&&&&studentList.add(student); &&&&&&&&&&&&&} &&&&&&&&&} &&&&&&&&&catch&(FileNotFoundException&e) &&&&&&&&&{ &&&&&&&&&&&&&throw&new&Exception(&源文件未找到&,&e); &&&&&&&&&} &&&&&&&&&catch&(IOException&e) &&&&&&&&&{ &&&&&&&&&&&&&throw&new&Exception(&读文件异常.&,&e); &&&&&&&&&} &&&&&&&&&finally&&&&&&&&&{&&&&&&&&&&&&&if&(br&!=&null) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&try&&&&&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&br.close(); &&&&&&&&&&&&&&&&&} &&&&&&&&&&&&&&&&&catch&(IOException&e) &&&&&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&e.printStackTrace(); &&&&&&&&&&&&&&&&&} &&&&&&&&&&&&&} &&&&&&&&&} &&&&&&&&&return&studentL &&&&&} &&&&&&&&&&&&&&&&&&public&void&writeFile(List&StudentInfo&&studentList,&String&dstPath)&throws&Exception &&&&&{ &&&&&&&&&BufferedWriter&bw&=&null; &&&&&&&&&try&&&&&&&&&{ &&&&&&&&&&&&&bw&=&new&BufferedWriter(new&FileWriter(dstPath)); &&&&&&&&&&&&&if&(studentList&!=&null&&&&!studentList.isEmpty()) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&for(StudentInfo&stu:studentList) &&&&&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&bw.write(stu.getId()+&,&+stu.getName()+&,&+stu.getBirthday()+&,&+stu.getScore()); &&&&&&&&&&&&&&&&&&&&&bw.newLine();&&&&&&&&&&&&&&&&&} &&&&&&&&&&&&&} &&&&&&&&&&&&&bw.flush();&&&&&&&&&} &&&&&&&&&catch&(IOException&e) &&&&&&&&&{ &&&&&&&&&&&&&throw&new&Exception(&目标文件未找到&,&e); &&&&&&&&&} &&&&&&&&&finally&&&&&&&&&{&&&&&&&&&&&&&&&&if&(bw&!=&null) &&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&try&&&&&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&bw.close(); &&&&&&&&&&&&&&&&&} &&&&&&&&&&&&&&&&&catch&(IOException&e) &&&&&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&e.printStackTrace(); &&&&&&&&&&&&&&&&&} &&&&&&&&&&&&&} &&&&&&&&&} &&&&&} &}&
3.编写main方法
package&com. &&import&java.io.F &import&java.util.ArrayL &import&java.util.C &import&java.util.L &&public&class&Test &{ &&&&&&&&&&&&&&&&&public&static&void&main(String[]&args)&throws&Exception &&&&&{ &&&&&&&&&String&srcPath&=&&C:&&+&File.separator&+&&test&&+&File.separator&+&&student.txt&; &&&&&&&&&String&dstPath&=&&C:&&+&File.separator&+&&test&&+&File.separator&+&&result.txt&; &&&&&&&&&&&&&&&&&&StudentFile&fileStu&=&new&StudentFile(); &&&&&&&&&List&StudentInfo&&studentList&=&fileStu.readFile(srcPath); &&&&&&&&&&&&&&&&&&List&StudentInfo&&tempList&=&new&ArrayList&StudentInfo&(); &&&&&&&&&for&(int&i&=&studentList.size()-1;&i&&&0;&i--) &&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&&&&&&tempList.add(studentList.get(i)); &&&&&&&&&&&&&studentList.remove(i); &&&&&&&&&} &&&&&&&&&&&&&&&&&&Collections.sort(tempList); &&&&&&&&&for&(int&i=0,n&=&tempList.size();&i&&&n;&i++) &&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&&&&&&studentList.add(tempList.get(i)); &&&&&&&&&} &&&&&&&&&&&&&&&&&&fileStu.writeFile(studentList,&dstPath); &&&&&} &}&
&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)8961人阅读
javaSE(14)
import java.io.BufferedW & & & & & & & & & & & & & & & & & & & & & & & & & & &&
import java.io.FileOutputS & & & & & & & & & & & & & & & & & & & & & & & & & &&
import java.io.FileW & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
import java.io.IOE & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
import java.io.OutputStreamW & & & & & & & & & & & & & & & & & & & & & & & & &&
import java.io.RandomAccessF & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//比较常用 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
public class WriteStreamAppend { & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & public static void method1(String file, String conent) { & & & & & & & & & & & &&
& & & & BufferedWriter out = & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & try { & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &out = new BufferedWriter(new OutputStreamWriter( & & & & & & & & & & & &
& & & & & & & & & & new FileOutputStream(file, true))); & & & & & & & & & & & & & & &
& & & & & & &out.write(conent); & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &} catch (Exception e) { & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & &e.printStackTrace(); & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &} finally { & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & try { & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & &out.close(); & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &} catch (IOException e) { & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & &e.printStackTrace(); & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & public static void method2(String fileName, String content) { & & & & & & & & & &
& & & & try { & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 & & &
& & & & & & &FileWriter writer = new FileWriter(fileName, true); & & & & & & & & & &&
& & & & & & &writer.write(content); & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &writer.close(); & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & &} catch (IOException e) { & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & &e.printStackTrace(); & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & public static void method3(String fileName, String content) { & & & & & & & & & &
& & & & try { & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & // 打开一个随机访问文件流,按读写方式 & & & & & & & & & & & & & & & & & &
& & & & & & &RandomAccessFile randomFile = new RandomAccessFile(fileName, &rw&); & &&
& & & & & & // 文件长度,字节数 & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & long fileLength = randomFile.length(); & & & & & & & & & & & & & & & & &&
& & & & & & // 将写文件指针移到文件尾。 & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &randomFile.seek(fileLength); & & & & & & & & & & & & & & & & & & & & & &
& & & & & & &randomFile.writeBytes(content); & & & & & & & & & & & & & & & & & & & &&
& & & & & & &randomFile.close(); & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & &} catch (IOException e) { & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & &e.printStackTrace(); & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & public static void main(String[] args) { & & & & & & & & & & & & & & & & & & & &&
& & & & &System.out.println(&start&); & & & & & & & & & & & & & & & & & & & & & & & &
& & & & &method1(&c:/test.txt&, &追加到文件的末尾&); & & & & & & & & & & & & & & & &&
& & & & &System.out.println(&end&); & & & & & & & & & & & & & & & & & & & & & & & & &
& & &} & & & & & & & & & & & & & & & & & & & & & & & & & & & &&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:799281次
积分:6580
积分:6580
排名:第3783名
原创:145篇
转载:65篇
评论:58条
(1)(4)(1)(1)(3)(2)(4)(6)(3)(2)(1)(2)(2)(8)(14)(15)(4)(1)(1)(2)(1)(6)(15)(2)(5)(1)(9)(29)(11)(6)(3)(4)(20)(21)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'&&&原创文章,转载请注明出处!
------------------------------------------------------------
&& &今天接到一个项目需求变更,是关于从数据库查询到30W条数据放到一个List中,然后使用StringBuffer把List取出放入到StringBuffer中。最后使用 BufferedWriter.write(buffer.toString());添加到文件中,此时会消耗几个小时才能把数据存放到文件中。
项目源码:
1 public void generateCommonSMSFile(String localFilePath,String date) throws Exception{
List smsList = getSingleDataSMS("03",date);
List listid = new ArrayList();
List subList = new ArrayList();
StringBuffer buffer = new StringBuffer("S|2|S0355||||");
buffer.append(smsList.size() + "|");
buffer.append("\r\n");
if(smsList != null && smsList.size() != 0) {
for(int i = 0;i & smsList.size();i++) {
OmsSingleDataDTO msdd = (OmsSingleDataDTO)smsList.get(i);
String transDate = msdd.getTransDate();
String leastDate = msdd.getLeastDate();
buffer.append(String.valueOf(i+1));
buffer.append("|");
buffer.append(msdd.getMobile());
buffer.append("|");
buffer.append(msdd.getCustNo());
buffer.append("|");
buffer.append(msdd.getAdvMemo1());
buffer.append("|");
buffer.append(CommonUtil.deleteZero(transDate.substring(5, 7))+"月"+CommonUtil.deleteZero(transDate.substring(8))+"日");
buffer.append("|");
buffer.append(msdd.getTransAmount());
buffer.append("|");
buffer.append(CommonUtil.deleteZero(leastDate.substring(4, 6))+"月"+CommonUtil.deleteZero(leastDate.substring(6))+"日");
buffer.append("|");
buffer.append("FQ"+msdd.getConsumeCode());
buffer.append("|");
buffer.append(msdd.getInstNum());
buffer.append("|");
buffer.append(msdd.getFirstAmount());
buffer.append("|");
buffer.append(msdd.getEachAmount());
buffer.append("|");
buffer.append(msdd.getTotalPoundage());
buffer.append("|");
buffer.append(msdd.getAdvMemo2());
buffer.append("|");
buffer.append("\r\n");
subList.add(msdd.getSeqId());
if((i+1)%5000==0){
listid.add(subList);
subList=new ArrayList();
listid.add(subList);
File file1 = FileUtils.createFile(localFilePath);
BufferedWriter bw = FileUtils.getWrite(file1);
bw.write(buffer.toString());
bw.close();
if(listid != null && listid.size() &= 1) {
for(int i=0;i&listid.size();i++) {
("普通消费类 - 第"+ (i+1) +"次运行开始");
long msBefore = System.currentTimeMillis();
updateMosSingleData((List)listid.get(i));
long msAfter = System.currentTimeMillis();
("普通消费类 - 第"+ (i+1) +"次运行结束,共耗时" + (msAfter - msBefore) + "毫秒");
} catch (Exception e) {
OMSLogger.error("生成单笔分期批量短信文件异常");
throw new Exception("生成单笔分期批量短信文件异常",e);
查询数据库,返回list供上面的数据写入文件的方法使用
1 private List getSingleDataSMS(String instFlag,String vdate) throws PafaDAOException {
List SmsList = new ArrayList();
List list = new ArrayList();
OmsSingleDataDTO msdd = null;
list = executeSqlDao.queryForList("SELECT-SMS-SingleData-LIST",null);
HashMap map =new HashMap();
map.put("instFlag", instFlag);
map.put("vdate", vdate);
int total = 0;
total = ((Integer)executeSqlDao.queryForObject("SELECT-SMS-SingleData-COUNT", map)).intValue();
防止大于1万条时出错,循环处理
for(int i=0; i& i+=5000){
HashMap paramMap =new HashMap();
List rs = new ArrayList();
paramMap.put("startNum", String.valueOf(i+1));
paramMap.put("endNum", String.valueOf(i+5000));
paramMap.put("instFlag", instFlag);
paramMap.put("vDate", vdate);
rs = executeSqlDao.queryForList("SELECT-SMS-SingleData-LIST",paramMap);
if (rs!=null){
for(int j=0; j&rs.size(); j++){
list.add(rs.get(j));
if(list != null && list.size() &= 1) {
Set set = commonService.getLogoValue(null);
for(int m = 0;m & list.size();m++) {
msdd = (OmsSingleDataDTO)list.get(m);
if(!OrderBaseService.getLogIsValid(msdd.getCardNo(),"14",set)) {
// 更新处理状态
this.updateMosSingleData2(msdd.getSeqId());
String custNo = msdd.getCustNo();
custNo = custNo.length() &= 12 ? custNo.substring(custNo.length() - 12):custNo;
msdd.setCustNo(custNo);
SmsList.add(msdd);
return SmsL
以上是使用的一次全部查询出数据,然后一次写入到文件中,但是这种方法会用几个小时才能完全写入。下面是自己优化后的方案:
1 public void generateSMSFile(String localFilePath,String date) throws Exception{
File file1 = FileUtils.createFile(localFilePath);
BufferedWriter bw = FileUtils.getWrite(file1);
StringBuffer buffer = new StringBuffer("S|1|S0180||||");
List smslistCount = getSingleDataSMS("03", date);
buffer.append(smslistCount.size() + "|");
buffer.append("\r\n");
List subList = new ArrayList();
List listid = new ArrayList();
int count = 1;
int total = 0;
total = generateSMSFilestpeSize(date);
//防止大于1万条时出错,循环处理
for(int k=0; k& k+=5000){
List smsList = getSingleDataSMSstep("03", date, k, k+5000);
if(smsList != null && smsList.size() != 0) {
for(int i = 0;i & smsList.size();i++) {
OmsSingleDataDTO msdd = (OmsSingleDataDTO)smsList.get(i);
String transDate = msdd.getTransDate();
String leastDate = msdd.getLeastDate();
count = count +1;
buffer.append(String.valueOf(count));
buffer.append("|");
buffer.append(msdd.getMobile());
buffer.append("|");
buffer.append(msdd.getCustNo());
buffer.append("|");
buffer.append(transDate.substring(5,7));
buffer.append("|");
buffer.append(transDate.substring(8,10));
buffer.append("|");
buffer.append(msdd.getTransAmount());
buffer.append("|");
buffer.append(msdd.getFirstAmount());
buffer.append("|");
buffer.append(msdd.getEachAmount());
buffer.append("|");
buffer.append(leastDate.substring(4,6));
buffer.append("|");
buffer.append(leastDate.substring(6,8));
buffer.append("|");
buffer.append(msdd.getConsumeCode());
buffer.append("|");
buffer.append("\r\n");
subList.add(msdd.getSeqId());
if((i+1)%5000==0){
listid.add(subList);
subList=new ArrayList();
listid.add(subList);
bw.write(buffer.toString());
buffer = new StringBuffer();
if(listid != null && listid.size() &= 1) {
for(int i=0;i&listid.size();i++) {
("保费类 - 第"+ (count) +"次运行开始");
long msBefore = System.currentTimeMillis();
updateMosSingleData((List)listid.get(i));
long msAfter = System.currentTimeMillis();
("保费类 - 第"+ (count) +"次运行结束,共耗时" + (msAfter - msBefore) + "毫秒");
bw.close();
} catch (Exception e) {
OMSLogger.error("生成单笔分期批量短信文件异常");
throw new Exception("生成单笔分期批量短信文件异常",e);
每次获取5000条数据返回list,写入文件之后再次执行该方法在获取5000条数据
private List getSingleDataSMSstep(String instFlag,String vdate,int startNum,int endNum) throws PafaDAOException {
List SmsList = new ArrayList();
List list = new ArrayList();
OmsSingleDataDTO msdd = null;
HashMap paramMap =new HashMap();
List rs = new ArrayList();
paramMap.put("startNum", startNum);
paramMap.put("endNum", endNum);
paramMap.put("instFlag", instFlag);
paramMap.put("vDate", vdate);
rs = executeSqlDao.queryForList("SELECT-SMS-SingleData-LIST",paramMap);
if (rs!=null){
for(int j=0; j&rs.size(); j++){
list.add(rs.get(j));
if(list != null && list.size() &= 1) {
Set set = commonService.getLogoValue(null);
for(int m = 0;m & list.size();m++) {
msdd = (OmsSingleDataDTO)list.get(m);
if(!OrderBaseService.getLogIsValid(msdd.getCardNo(),"14",set)) {
// 更新处理状态
this.updateMosSingleData2(msdd.getSeqId());
String custNo = msdd.getCustNo();
custNo = custNo.length() &= 12 ? custNo.substring(custNo.length() - 12):custNo;
msdd.setCustNo(custNo);
SmsList.add(msdd);
return SmsL
&补充一个自己写的小例子:
1 package myTempT
3 import java.io.BufferedW
4 import java.io.F
5 import java.io.FileW
6 import java.io.IOE
8 public class ioTestBuffer {
public static void main(String[] args) throws IOException {
File file1 = new File("d:\\io\\out.txt");//最终写入文件地址
BufferedWriter bw = new BufferedWriter(new FileWriter(file1));
StringBuffer buffer = new StringBuffer("");
for (int j = 0; j & 30; j++) {
//之所以I循环1W次就写出一次,是因为buffer超过1W次append之后极容易在次append的时候报错。
for (int i = 0; i & 10000; i++) {
buffer.append(i+""+i+""+i+""+i+""+i+""+i+""+i+""+i+""+i+""+i+"");
buffer.append("\r\n");//换行
bw.write(buffer.toString());//1W次I循环结束 写入到文件中
buffer = new StringBuffer("");//格式化buffer。
buffer.append(j+"-------------------------------------");//第 j 此循环加上个记号
buffer.append("\r\n");//一次J循环结束 换行
bw.close();
阅读(...) 评论()}

我要回帖

更多关于 java bufferedwriter 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信