欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java导出txt文件的方法

程序员文章站 2024-03-03 18:06:46
本文实例讲述了java导出txt文件的方法。分享给大家供大家参考。具体如下: 例子一 /** * export导出文件 */ @requestmap...

本文实例讲述了java导出txt文件的方法。分享给大家供大家参考。具体如下:

例子一

/** 
* export导出文件 
*/ 
@requestmapping(value="/grab/export/csv",method={requestmethod.get}) 
public void exportcsv(httpservletrequest request,httpservletresponse response){
  string userid = servletrequestutils.getstringparameter(request, "userid", "test"); 
  modelandview mav=new modelandview(); 
  sqlvideolist sqlvideolist =new sqlvideolist(); 
  list<videolistmodel> list = new arraylist<videolistmodel>(); 
  try { 
   list = sqlvideolist.selectsuccessdate(userid); 
  } catch (sqlexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
  //导出txt文件 
  response.setcontenttype("text/plain");  
  string filename="videolist"; 
  try { 
   filename = urlencoder.encode("videolist", "utf-8"); 
  } catch (unsupportedencodingexception e1) { 
   // todo auto-generated catch block 
   e1.printstacktrace(); 
  }  
  response.setheader("content-disposition","attachment; filename=" + filename + ".txt");  
  bufferedoutputstream buff = null;   
  stringbuffer write = new stringbuffer();   
  string enter = "\r\n";   
  servletoutputstream outstr = null;   
  try {   
    outstr = response.getoutputstream(); // 建立   
    buff = new bufferedoutputstream(outstr); 
    //把内容写入文件 
    if(list.size()>0){ 
     for (int i = 0; i < list.size(); i++) { 
      write.append(list.get(i).geturl()+","); 
      write.append(list.get(i).gettitle()); 
      write.append(enter);   
     } 
    } 
    buff.write(write.tostring().getbytes("utf-8"));   
    buff.flush();   
    buff.close();   
  } catch (exception e) {   
   e.printstacktrace();   
  } finally {   
   try {   
    buff.close();   
    outstr.close();   
   } catch (exception e) {   
    e.printstacktrace();   
   }   
  } 
}

例子二:

/** 
 * 导出vip兑换码。 
 * @throws unsupportedencodingexception 
 */ 
@requestmapping(value = "/{exchangeid}/{packageid}/export", method = requestmethod.get) 
public void writetotxt(@pathvariable string exchangeid,@pathvariable string packageid, httpservletrequest request,httpservletresponse response) throws unsupportedencodingexception { 
 string schoolid = this.getschoolid(request); // 网校id 
 // 获取网校的vip套餐相应的兑换码 
 vipcodeexample example=new vipcodeexample(); 
 example.createcriteria().andschoolidequalto(schoolid).andpackageidequalto(packageid).andexchangeidequalto(exchangeid); 
 list<vipcode> vipcodes = vipexchangemanager.getvipcode(example); 
 if(vipcodes.size()>0){ 
   response.setcontenttype("text/plain");// 一下两行关键的设置
   response.addheader("content-disposition", 
     "attachment;filename="+java.net.urlencoder.encode(vipcodes.get(0).getname(),"utf-8")+".txt");
     // filename指定默认的名字
   vipcode vipcode=new vipcode(); 
   bufferedoutputstream buff = null; 
   stringbuffer write = new stringbuffer(); 
   string tab = "  "; 
   string enter = "\r\n"; 
   servletoutputstream outstr = null; 
   try { 
    outstr = response.getoutputstream();// 建立 
    buff = new bufferedoutputstream(outstr); 
    for (int i = 0; i < vipcodes.size(); i++) { 
     vipcode = vipcodes.get(i); 
     write.append(i+1); //序号 
     write.append(tab); 
     write.append(vipcode.getexchangecode()); 
     write.append(tab); 
     if("normal".equals(vipcode.getstatus())){ 
      write.append("正常");   
     }else{ 
      write.append("已兑换");   
     } 
     write.append(enter); 
    } 
    buff.write(write.tostring().getbytes("utf-8")); 
    buff.flush(); 
    buff.close(); 
   } catch (exception e) { 
    e.printstacktrace(); 
   } finally { 
    try { 
     buff.close(); 
     outstr.close(); 
    } catch (exception e) { 
     e.printstacktrace(); 
    } 
   } 
  } 
}

希望本文所述对大家的java程序设计有所帮助。