zoukankan      html  css  js  c++  java
  • 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("
    ");
                    }
                }
                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();
                    }
                }
  • 相关阅读:
    python使用zipfile递归压缩和解压缩文件
    文件上传控件bootstrap-fileinput中文设置没有效果的情况
    vue keep-alive 不生效和多级(三级以上)缓存失败
    Entityframework批量删除
    EasyUI ComboGrid 集成分页、按键示例
    在Entity Framework中使用事务
    MVC实用构架设计(三)——EF-Code First(6):数据更新最佳实践
    Entityframework更新数据和插入数据
    esayui-datagrid的使用
    javascript:;与javascript:void(0)使用介绍
  • 原文地址:https://www.cnblogs.com/linhongwenBlog/p/9146158.html
Copyright © 2011-2022 走看看