导入导出文件

import java.io.*;
import java.util.*;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ImportExport{
    /**
     * 实现导入,将路径和文件名称返回一个字符串的数组
     * @param filePath String
     * @param fileName String
     * @return Vector
     */
    public Vector getReadFile(String filePath,String fileName){
        Vector vec= new Vector();
        String str="";
        String files = filePath + fileName;
        File file = new File(files);
        if (!file.exists()) {
            return null;
        }
        try{
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            while((str=br.readLine())!=null){
                vec.add(str);
            }
            br.close();
            fr.close();
            return vec;
        }catch(Exception e){
            return null;
        }
    }

    /**
     * 实现导入,将文件流返回一个字符串的数组
     * @param stream InputStream
     * @return Vector
     */
    public Vector getReadFile(FormFile file){
        Vector vec= new Vector();
        String str="";
        try{
            InputStream stream = file.getInputStream();
            InputStreamReader isr = new InputStreamReader(stream);
            BufferedReader br = new BufferedReader(isr);
            while((str=br.readLine())!=null){
                vec.add(str);
            }
            br.close();
            isr.close();
            stream.close();
            return vec;
        }catch(Exception e){
            return null;
        }
    }


    /**
     * 实现导出,传入一个字符串数组
     * @param filePath String
     * @param fileName String
     * @param strList String[]
     * @return boolean 写文件成功返回true,否则false
     */
    public boolean setWriteFile(String filePath, String fileName, String[] strList) {
        if(strList==null){
            return false;
        }
        String files = filePath + fileName;
        File file = new File(files);
        try {
            if (!file.exists()) { //如果文件不存在,就创建文件
                file.createNewFile();
            }
            FileWriter fWriter = new FileWriter(file, true);
            PrintWriter out = new PrintWriter(fWriter);
            for (int i = 0; i < strList.length; i++) {
                out.println(strList[i]);
            }
            out.close();
            fWriter.close();
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 实现导出,写文件头
     * @param filePath String
     * @param fileName String
     * @param strList String[]
     * @return boolean 写文件成功返回true,否则false
     */
    public boolean setWriteFileTitle(String filePath, String fileName, String fileTitle) {

        String files = filePath + fileName;
        File file = new File(files);
        try {
            if (!file.exists()) { //如果文件不存在,就创建文件
                file.createNewFile();
            }
            FileWriter fWriter = new FileWriter(file, true);
            PrintWriter out = new PrintWriter(fWriter);
            out.println(fileTitle);
            out.close();
            fWriter.close();
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 返回一个数组
     * @param str String
     * @return String[]
     */
    public String[] getList(String str,int number){
        int num=0;
        for(int i=0;i<str.length();i++){
            if(",".equals(str.charAt(i)+"")){
                num++;
            }
        }
        num++;//在得出的中的","号的数量上加1,就是数组的长度
        if(number!=num){
            return null;
        }
        String[] list=str.split(",",number);
        return list;
    }

    /**
     * 根据数据库中的数据下载成文件
     * @param response HttpServletResponse
     * @param fileName String
     * @param strList String[]
     * @throws Exception
     */
    public void downFile(HttpServletResponse response,String fileName,String[] strList)throws Exception{
        //初始化流
        OutputStream outs = response.getOutputStream();
        DataOutputStream dateOut = new DataOutputStream(outs);
        //设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition",
                           "attachment; filename=/"" + fileName + "/"");

        //循环取出流中的数据
        String str = "";
        for (int i = 0; i < strList.length; i++) {
            str = new String(strList[i].getBytes("GB2312"), "ISO8859-1");
            dateOut.writeBytes(str + "/r/n");
        }
        //关闭流
        dateOut.close();
        outs.close();
    }

    /**
     * 根据数据库中的数据下载成文件
     * @param response HttpServletResponse
     * @param fileName String
     * @param fileTitle String
     * @param strList String[]
     * @throws Exception
     */
    public void downFile(HttpServletResponse response,String fileName,String fileTitle,String[] strList)throws Exception{
        //初始化流
        OutputStream outs = response.getOutputStream();
        DataOutputStream dateOut = new DataOutputStream(outs);
        //设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition",
                           "attachment; filename=/"" + fileName + "/"");

        //循环取出流中的数据
        String str = "";
        fileTitle=new String(fileTitle.getBytes("GB2312"), "ISO8859-1");
        dateOut.writeBytes(fileTitle + "/r/n");
        for (int i = 0; i < strList.length; i++) {
            str = new String(strList[i].getBytes("GB2312"), "ISO8859-1");
            dateOut.writeBytes(str + "/r/n");
        }
        //关闭流
        dateOut.close();
        outs.close();
    }


    public static void main(String[] args){
        String[] strList={"aa,bb,cc","11,22,33","dd,ee,ff"};
        String filePath="D:/";
        String fileName="test1.csv";
        //ImportExport.setWriteFile(filePath,fileName,strList);
        ImportExport ie=new ImportExport();
        Vector vec=ie.getReadFile(filePath,fileName);

        if(vec!=null){
            for(int i=0;i<vec.size();i++){
                String str=(String)vec.get(i);
                String[] list=ie.getList(str,4);
                if(list!=null){
                    System.out.println(str + ";length=" + list.length);
                    for (int j = 0; j < list.length; j++) {
                        System.out.println("str" + j + "=" + list[j]);
                    }
                }
            }
        }else{
            System.out.println("vec is null");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值