BufferWriter写出csv文件

1、第一次发现写入csv文件是多么的方便呀。无需poi那种格式复杂。

import java.io.*;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
public class Demo1 {
    public static void main(String[] args)  {
        File f = new File("D://aa.csv");
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ReadCsv(f);
        //WriteCSV(f);
    }

    /**
     * 文件读取
     * @param file
     */
    public static void ReadCsv(File file){

        BufferedReader bufferedReader = null;
        ArrayList<String> list = new ArrayList<>();
        try {
            bufferedReader= new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
             String line = null;//一行一行读取
            while ((line = bufferedReader.readLine()) !=null) {
                    list.add(line);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedReader!= null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(list);
    }

    /**
     * 文件写出
     * @param file
     */
    public static void WriteCSV(File file){
        OutputStreamWriter write = null;
        BufferedWriter writer = null;
        try {
            //fasle 代表覆盖文件中的原始内容  true 代表对原始文件进行增加
            write = new OutputStreamWriter(new FileOutputStream(file,false));
            writer = new BufferedWriter(write);
            //  "\r\n" 代表换行 “”
            writer.write("id"+","+"username"+","+"password"+"\r\n");
            for (int i = 0; i < 9; i++) {
                writer.write("a"+i+","+"a"+i+","+"a"+i+"\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer!= null){
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (write!= null){
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //文件写出到浏览器通过流
 @GetMapping("export")
    public static void WriteCSV( HttpServletRequest request,HttpServletResponse response)  {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            list.add("a"+i+","+"a"+i+","+"a"+i+"\r\n");
        }
        String str = "";
        for (String s : list) {
            str=str+s;
        }
        OutputStream o = null;
        try {
            response.setContentType("application/csv");
            response.setContentType("application/download;charset=GBK");
            response.setContentType("Content-type:application/vnd.ms-excel;charset=GBK");
            response.setHeader("Content-disposition","attachment;filename=\"" + new Date() + ".csv\"");
            o = response.getOutputStream();
            //处理中文乱码
            out.write(new byte[]{(byte) 0xEF, (byte) 0xBB,(byte) 0xBF});
            o.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (o!=null){
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值