从数据库取出的内容是一个List,如何写进文本中?用java实现

解决方案 »

  1.   

    list.get的方法啊,一个个读出来
    Customerinformation list = new Customerinformation();
    list.getCustomerCompany();
      

  2.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication3;import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;/**
     *
     * @author Administrator
     */
    public class NewClass {    public static void main(String[] args) {
            ArrayList al = new ArrayList();
            for (int i = 0; i < 100; i++) {
                al.add(new Integer(i));
            }
            list2file(al, "C:\\A.TXT");
        }    public static boolean list2file(List l, String filepath) {
            if (null == l) {
                return false;
            }        FileWriter fr = null;
            try {            fr = new FileWriter(filepath);
                Iterator it = l.iterator();
                while (it.hasNext()) {
                    fr.write(it.next().toString());
                    fr.write("\n");
                }
                fr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (null != fr) {
                        fr.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return true;
        }
    }
      

  3.   

    可以,那如何让客户机可以自动下载这个txt文件呢?
      

  4.   

    public ActionForward printall(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws IOException {
    List all = null;
    try {
    currentPage = Integer.parseInt(request.getParameter("cp"));
    } catch (Exception e) {
    }
    try {
    all = this.iuserworddao.queryAll();

    catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    String path=request.getRealPath(".");

    FileWriter fw=null;
            try {           fw=new FileWriter(path + "\\WriteData.txt"); 
                int i;
                for(i=0;i<all.size();i++){
         Userword pm = (Userword)all;
        
              fw.write(pm.getUserword());
               fw.write(pm.getUsertran());
               fw.write("\n");
                }
                fw.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (null != fw) {
                        fw.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
           request.setAttribute("all", all);
                  return mapping.findForward("list");
    }
    这段代码哪错了?竟然报错:java.lang.ClassCastException: java.util.ArrayList
      

  5.   

    上面的问题已经搞定,就是怎么能让客户端弹出要求下载的txt呢?
      

  6.   

    使用输出流就可以解决了,你可以outputStream  out =response.getOutputStream();
    然后输出就可以了
      

  7.   

    就是我这个txt在本机上可以生成,但是在客户机上就不行了,怎么能在客户机上实现生成、下载txt文档呢?
      

  8.   

    1.把list里面的对象一个个的取出来
    2.把取出来的对象一个个的拆封
    3.通过java I/O写入到文本文档