我现在把一个数据集的里面的数据倒出来,但是解析出来的数据条数少了,比如我要到出的是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();
}

}

解决方案 »

  1.   

    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);}
    fw.flush();//加入
    fw.close();//加入
    } catch (IOException e) {
    e.printStackTrace();
    } }
      

  2.   

    txt 能容纳的数据量是很大的,他比excel   还能装,解析这快,这种情况有可能数据没解析完  但我不知道什么原因
      

  3.   

    请flush--刷新该流的缓冲;
    close--关闭该流.和数据解析没有关系的。
      

  4.   

    我已经加上刷新该流的缓冲了  也关闭了  还是没解决我的问题现在控制台的错误是:
    严重: 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)
      

  5.   

    for(int i=0;i<=dataSource.size();++i){
    这里改成
    for(int i=0;i<dataSource.size();i++){
      

  6.   

    for(int i=0;i<=dataSource.size();++i)
    你确实要 <= 吗?
    你确定要++i 吗?
      

  7.   

    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); }这么循环应该是没问题 少了一百多条?你看看是不是键不对啊 
      

  8.   

    谢谢大家的支持,我现在正确代码如下:
    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) {
    }
    }

    } 其实我主要错在吧刷新和关闭的地方用错了,不小心的后果