我现在把一个数据集的里面的数据倒出来,但是解析出来的数据条数少了,比如我要到出的是6948
但到导出的txt文件看 少了100多条以下是我写方法:
public void financeWriter(List dataSource,String file){
FileWriter fw=null;
StringBuffer row=null;
try {
fw=new FileWriter(file);
//循环行
for(int i=0;i<=dataSource.size();++i){
System.out.println("------------------"+dataSource.size());
row=new StringBuffer();
Map map= (HashMap)dataSource.get(i);
//输出列
row.append((String)map.get("costIndexId")+",");
row.append((String)map.get("indexId")+",");
row.append((String)map.get("custId")+",");
row.append((String)map.get("charge")+",");
row.append((String)map.get("costCenterId")+",");
row.append((String)map.get("bureauNo")+",");
row.append((String)map.get("strateGroup")+"\n");
fw.write(row.toString());
System.out.println("-----------=====-------"+i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
但到导出的txt文件看 少了100多条以下是我写方法:
public void financeWriter(List dataSource,String file){
FileWriter fw=null;
StringBuffer row=null;
try {
fw=new FileWriter(file);
//循环行
for(int i=0;i<=dataSource.size();++i){
System.out.println("------------------"+dataSource.size());
row=new StringBuffer();
Map map= (HashMap)dataSource.get(i);
//输出列
row.append((String)map.get("costIndexId")+",");
row.append((String)map.get("indexId")+",");
row.append((String)map.get("custId")+",");
row.append((String)map.get("charge")+",");
row.append((String)map.get("costCenterId")+",");
row.append((String)map.get("bureauNo")+",");
row.append((String)map.get("strateGroup")+"\n");
fw.write(row.toString());
System.out.println("-----------=====-------"+i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fw=null;
StringBuffer row=null;
try {
fw=new FileWriter(file);
//循环行
for(int i=0;i<=dataSource.size();++i){
System.out.println("------------------"+dataSource.size());
row=new StringBuffer();
Map map= (HashMap)dataSource.get(i);
//输出列
row.append((String)map.get("costIndexId")+",");
row.append((String)map.get("indexId")+",");
row.append((String)map.get("custId")+",");
row.append((String)map.get("charge")+",");
row.append((String)map.get("costCenterId")+",");
row.append((String)map.get("bureauNo")+",");
row.append((String)map.get("strateGroup")+"\n");
fw.write(row.toString());
System.out.println("-----------=====-------"+i);}
fw.flush();//加入
fw.close();//加入
} catch (IOException e) {
e.printStackTrace();
} }
close--关闭该流.和数据解析没有关系的。
严重: Servlet.service() for servlet default threw exception
java.lang.IndexOutOfBoundsException: Index: 155, Size: 155
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
这里改成
for(int i=0;i<dataSource.size();i++){
你确实要 <= 吗?
你确定要++i 吗?
System.out.println("------------------" + dataSource.size());
row = new StringBuffer();
Map map = (HashMap) dataSource.get(i);
// 输出列
row.append((String) map.get("costIndexId") + ",");
row.append((String) map.get("indexId") + ",");
row.append((String) map.get("custId") + ",");
row.append((String) map.get("charge") + ",");
row.append((String) map.get("costCenterId") + ",");
row.append((String) map.get("bureauNo") + ",");
row.append((String) map.get("strateGroup") + "\n");
fw.write(row.toString());
System.out.println("-----------=====-------" + i); }这么循环应该是没问题 少了一百多条?你看看是不是键不对啊
public void financeWriter(List dataSource,String file){
FileWriter fw=null;
try {
fw=new FileWriter(file);
//循环行
for(int i=0;i<dataSource.size();i++){
StringBuilder row = new StringBuilder();
Map map= (HashMap)dataSource.get(i);
//输出列
row.append((String)map.get("costIndexId")+",");
row.append((String)map.get("indexId")+",");
row.append((String)map.get("custId")+",");
row.append((String)map.get("charge")+",");
row.append((String)map.get("costCenterId")+",");
row.append((String)map.get("bureauNo")+",");
row.append((String)map.get("strateGroup")+"\n");
fw.write(row.toString());
}
System.out.println("共导出数据:"+dataSource.size()+"条");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fw != null)
fw.close();
} catch (IOException e) {
}
}
} 其实我主要错在吧刷新和关闭的地方用错了,不小心的后果