txt文档操作(导出和导入)【工具包系列】

上一篇介绍了CSV的导入导出,现在介绍txt的导入导出,两者非常的相似,就不多介绍了,直接上代码了

[java]  view plain  copy
  1. package com.lwl.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.FileReader;  
  9. import java.io.IOException;  
  10. import java.io.OutputStream;  
  11. import java.io.OutputStreamWriter;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14.   
  15. /** 
  16.  * txt文档操作(导出和导入) 
  17.  * @author liuweilong@zhicall.com   
  18.  * @create 2016-4-27 上午9:47:27 
  19.  * @version 1.0 
  20.  */  
  21. public class TextUtils {  
  22.   
  23.     /** 
  24.      * 导出 
  25.      * @param file Txt文件(路径+文件名),Txt文件不存在会自动创建 
  26.      * @param dataList  数据 
  27.      * @param heads  表头 
  28.      * @return 
  29.      * @author liuweilong@zhicall.com   
  30.      * @create 2016-4-27 上午9:49:49 
  31.      */  
  32.     public static boolean exportTxt(File file, List<String> dataList,String heads){  
  33.         FileOutputStream out=null;  
  34.          try {  
  35.             out = new FileOutputStream(file);  
  36.          return exportTxtByOS(out, dataList, heads);  
  37.         } catch (FileNotFoundException e) {  
  38.             e.printStackTrace();  
  39.             return false;  
  40.         }  
  41.           
  42.     }  
  43.       
  44.     /** 
  45.      * 导出 
  46.      * @param out 输出流 
  47.      * @param dataList  数据 
  48.      * @param heads  表头 
  49.      * @return 
  50.      * @author liuweilong@zhicall.com   
  51.      * @create 2016-4-27 上午9:49:49 
  52.      */  
  53.      public static boolean exportTxtByOS(OutputStream out, List<String> dataList,String heads){  
  54.             boolean isSucess=false;  
  55.               
  56.             OutputStreamWriter osw=null;  
  57.             BufferedWriter bw=null;  
  58.             try {  
  59.                 osw = new OutputStreamWriter(out);  
  60.                 bw =new BufferedWriter(osw);  
  61.                 //循环表头  
  62.                 if(heads!=null&&!heads.equals("")){  
  63.                      bw.append(heads).append("\r");  
  64.                 }  
  65.                 //循环数据  
  66.                 if(dataList!=null && !dataList.isEmpty()){  
  67.                     for(String data : dataList){  
  68.                         bw.append(data).append("\r");  
  69.                     }  
  70.                 }  
  71.                 isSucess=true;  
  72.             } catch (Exception e) {  
  73.                 e.printStackTrace();  
  74.                 isSucess=false;  
  75.             }finally{  
  76.                 if(bw!=null){  
  77.                     try {  
  78.                         bw.close();  
  79.                         bw=null;  
  80.                     } catch (IOException e) {  
  81.                         e.printStackTrace();  
  82.                     }   
  83.                 }  
  84.                 if(osw!=null){  
  85.                     try {  
  86.                         osw.close();  
  87.                         osw=null;  
  88.                     } catch (IOException e) {  
  89.                         e.printStackTrace();  
  90.                     }   
  91.                 }  
  92.                 if(out!=null){  
  93.                     try {  
  94.                         out.close();  
  95.                         out=null;  
  96.                     } catch (IOException e) {  
  97.                         e.printStackTrace();  
  98.                     }   
  99.                 }  
  100.             }  
  101.               
  102.             return isSucess;  
  103.         }  
  104.   
  105.      /** 
  106.          * 导入 
  107.          *  
  108.          * @param file Txt文件(路径+文件) 
  109.          * @return 
  110.          */  
  111.         public static List<String> importTxt(File file){  
  112.             List<String> dataList=new ArrayList<String>();  
  113.             BufferedReader br=null;  
  114.             try {   
  115.                 br = new BufferedReader(new FileReader(file));  
  116.                 String line = "";   
  117.                 while ((line = br.readLine()) != null) {   
  118.                     dataList.add(line);  
  119.                 }  
  120.             }catch (Exception e) {  
  121.             }finally{  
  122.                 if(br!=null){  
  123.                     try {  
  124.                         br.close();  
  125.                         br=null;  
  126.                     } catch (IOException e) {  
  127.                         e.printStackTrace();  
  128.                     }  
  129.                 }  
  130.             }  
  131.             return dataList;  
  132.         }  
  133.   
  134.        
  135.     /** 
  136.      *  测试  
  137.      * @param args 
  138.      * @author liuweilong@zhicall.com   
  139.      * @create 2016-4-27 上午10:11:46 
  140.      */  
  141.      public static void main(String[] args) {  
  142.          //导出数据测试  
  143. //       List<String> dataList=new ArrayList<String>();  
  144. //          dataList.add("1,张三,男");  
  145. //          dataList.add("2,李四,男");  
  146. //          dataList.add("3,小红,女");  
  147. //          File file = new File("E:/test");  
  148. //          if(!file.exists()){  
  149. //              file.mkdir();  
  150. //          }  
  151. //          boolean isSuccess=TextUtils.exportTxt(new File("E:/test/ljq.txt"), dataList,"编码,姓名,性别");  
  152. //          System.out.println(isSuccess);  
  153.               
  154.         //导入数据测试     
  155.            
  156.          List<String> dataList= TextUtils.importTxt(new File("E:/test/ljq.txt"));  
  157.          for (String string : dataList) {  
  158.             System.out.println(string);  
  159.         }  
  160.            
  161.     }  
  162.        
  163. }  

测试用例已附上,请自行测试,可以直接在项目中使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值