当前位置:首页 > 开发 > 编程语言 > Java > 正文

java导出txt文件的例子

发表于: 2014-06-18   作者:知了ing   来源:转载   浏览:
摘要: 代码很简单就一个servlet,如下: package com.eastcom.servlet; import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.sql.Connection; import java.sql.Resu
代码很简单就一个servlet,如下:
package com.eastcom.servlet;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eastcom.jdbc.JdbcFactory;

public class ExportConstractTxt extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 response.setContentType("text/plain"); 
         String fileName = URLEncoder.encode("配置文件比对结果", "UTF-8");  
         response.setHeader("Content-Disposition","attachment; filename=" + fileName + ".txt");  
         String device_id=request.getParameter("device_id");
         String time_tamp=request.getParameter("time_tamp");
        // result =new String(result.getBytes("iso-8859-1"), "utf-8");
         String result=getDIFF(device_id,time_tamp);
         //result =new String(result.getBytes("iso-8859-1"), "gbk");
         System.out.println(result);
         BufferedOutputStream buff = null;     
         StringBuffer write = new StringBuffer();     
         String enter = "\r\n";     
         ServletOutputStream outSTr = null;     
         try {     
              outSTr = response.getOutputStream();  // 建立     
              buff = new BufferedOutputStream(outSTr);     
              write.append(result.replaceAll("\n", 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();     
            }     
        }     
	}
	private String getDIFF(String deviceID,String time){
		String result="";
		Connection Conn=null;
		ResultSet rSet=null;
		Statement stmt=null;
		try {
			Conn = JdbcFactory.getInstance().getConnection();
			stmt = Conn.createStatement();
			String sql ="select  diff "+
						"  from rm.RM_CONF_FILE_DIFF  "+
					"	 where device_id = '"+deviceID+"' and to_char(time_stamp, 'yyyy-mm-dd hh24:mi:mm') = '"+time+"' ";
			System.out.println("getDIFF sql >"+sql);
			rSet=stmt.executeQuery(sql);
			while(rSet.next()){
				result=rSet.getString("diff");
			}
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
			if(stmt!=null){
				try {
					stmt.close();
				} catch (Exception e) {
				}
			}
			if(Conn!=null){
				try {
					Conn.close();
				} catch (Exception e) {
				}
			}
		}
		return result;
	}
}

java导出txt文件的例子

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
FileInputStream public class FileInputStream extends InputStream 构造方法: FileInputStream(Fi
FileOutputStream public class FileOutputStream extends OutputStream 构造方法: FileOutputStrea
MySQL批量导入导出Excel、txt数据 1、Excel文件 1.1导出数据到EXCEL文件 select * into outfile "C:
  JAVA 读取txt文件内容   通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出
我们以一个实际例子来说明java对txt文件的读、写操作。 计算出差补助费用,补助规则如下: 在30天以
有时候需要处理大量的数据,且这些数据又存在于excel表格内,在平时的时候,我是非常喜欢这样的数据
运行效果: 代码: package com.io; import java.io.BufferedReader; import java.io.File; import
importjava.io.BufferedReader; importjava.io.File; importjava.io.FileInputStream; importjava.i
java中写.txt文件,如何实现换行 1.使用java中的转义符"\r\r" 这样在str后面就有换行了. 注意:\r,\
一、功能简述 点击“导出”按钮,弹出文件选择对话框,选择存放路径,填写文件名,点击“导出”按钮
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号