zoukankan      html  css  js  c++  java
  • 导出txt

    public class FileUtil {
    /**
    * 导出
    * @param file Txt文件(路径+文件名),Txt文件不存在会自动创建
    * @param dataList 数据
    * @param heads 表头
    */
    public static boolean exportTxt(File file, List<String> dataList, String heads){
    FileOutputStream out=null;
    try {
    out = new FileOutputStream(file);
    return exportTxtByOS(out, dataList, heads);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    return false;
    }
    }

    /**
    * 导出
    * @param out 输出流
    * @param dataList 数据
    * @param heads 表头
    */
    public static boolean exportTxtByOS(OutputStream out, List<String> dataList, String heads){
    boolean isSucess=false;
    OutputStreamWriter osw=null;
    BufferedWriter bw=null;
    try {
    osw = new OutputStreamWriter(out);
    bw =new BufferedWriter(osw);
    //循环表头
    if(heads!=null&&!heads.equals("")){
    bw.append(heads).append(" ");
    }
    //循环数据
    if(dataList!=null && !dataList.isEmpty()){
    for(String data : dataList){
    bw.append(data).append(" ");
    }
    }
    isSucess=true;
    } catch (Exception e) {
    e.printStackTrace();
    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;
    }

    }
    听说学习能够让青春永驻。
  • 相关阅读:
    Windbg学习 (0x0002) 命令基础
    Windbg学习 (0x0001) 安装与基本配置
    python 20day--装饰器详解
    python 19day--生成器详解
    python 18day--迭代器详解
    python 17day--内置函数
    python 16day--函数作用域与函数式编程
    python 15day--递归函数与匿名函数
    python 14day--函数
    python 13day--集合、字符串格式化
  • 原文地址:https://www.cnblogs.com/chenyf/p/8805763.html
Copyright © 2011-2022 走看看