JAVA文件操作

1. 导出CSV文件

    /**
     * @Author lin.hongwen
     * @Date 2018/05/29
     * 导出CVS文件到指定目录
     *
     * @param file csv文件(路径+文件名),csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsvFile(File file, List<String> dataList){
        boolean isSucess=false;

        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSucess;
    }

 

2.导出从指定目录下载文件

            OutputStream out = null;
            FileInputStream in = null;
            try {
                File file = new File(filePath+fileName);
                //把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码,中文不要太多,最多支持17个中文,因为header有150个字节限制。
                fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
                //告诉浏览器输出内容为流
                response.setContentType("application/octet-stream");
                //Content-Disposition中指定的类型是文件的扩展名,并且弹出的下载对话框中的文件类型图片是按照文件的扩展名显示的,点保存后,文件以filename的值命名,保存类型以Content中设置的为准。注意:在设置Content-Disposition头字段之前,一定要设置Content-Type头字段。
                response.addHeader("Content-Disposition", "attachment;filename="+fileName);
                //设置内容长度
                response.setHeader("Content-Length", String.valueOf(file.length()));
                out = response.getOutputStream();
                in = new FileInputStream(file);
                byte[] b = new byte[1024];
                int n;
                while((n=in.read(b))!=-1){
                    out.write(b, 0, n);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

 

posted @ 2018-06-06 17:10  林被熊烟岛  阅读(144)  评论(0编辑  收藏  举报