[file]IO常用工具类IOUtils(Java读文件、写文件、打Zip包)

[文件] IO常用工具类IOUtils(Java读文件,写文件,打Zip包)http://www.bieryun.com/1003.html


功能目录:

  1. 将输入流转换成字节流
  1. 将文件读取为一个字符串
  1. 以指定编码格式将输入流按行置入一个列表<字符串>
  1. 以GBK格式将输入流按行置入一个列表<字符串>
  1. 转换为每行补充指定换行符(例如: “\ n”, “</ BR>”)
  1. 将字符串转出到指定文件
  1. 将多个文件打成一个拉链包

 

源码:

[java]查看纯文本

  1.  amosryan.utility.file;
  2. import  java.io.BufferedReader;
  3. import  java.io.ByteArrayOutputStream;
  4. import  java.io.File;
  5. import  java.io.FileInputStream;
  6. import  java.io.FileOutputStream;
  7. import  java.io.FileWriter;
  8. import  java.io.IOException;
  9. import  java.io.InputStream;
  10. import  java.io.InputStreamReader;
  11. import  java.io.PrintWriter;
  12. import  java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipOutputStream;
  16. /**
  17.  * IO常用工具包
  18.  * @author amosryan
  19.  * @since 2010-06-03
  20.  */
  21. public class IOUtils {
  22.     /**
  23.      * 将输入流转换成字节流
  24.      * @param input
  25.      * @return
  26.      * @throws Exception
  27.      */
  28.     public static byte[] toBytes(InputStream input) throws Exception {
  29.         byte[] data = null;
  30.         try {
  31.             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  32.             byte[] b = new byte[1024];
  33.             int read = 0;
  34.             while ((read = input.read(b)) > 0) {
  35.                 byteOut.write(b, 0, read);
  36.             }
  37.             data = byteOut.toByteArray();
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         } finally {
  41.             input.close();
  42.         }
  43.         return data;
  44.     }
  45.     /**
  46.      * 将文件读取为一个字符串
  47.      * 
  48.      * @param input
  49.      * @return
  50.      * @throws Exception
  51.      */
  52.     public static String toString(File file) throws Exception {
  53.         return toString(new FileInputStream(file));
  54.     }
  55.     /**
  56.      * 将输入流转换为一个串
  57.      * 
  58.      * @param input
  59.      * @return
  60.      * @throws Exception
  61.      */
  62.     public static String toString(InputStream input) throws Exception {
  63.         return toStringWithLineBreak(input, null);
  64.     }
  65.     /**
  66.      * 以指定编码格式将输入流按行置入一个List<String>
  67.      * 
  68.      * @param input
  69.      * @return
  70.      * @throws Exception
  71.      */
  72.     public static List<String> toLines(InputStream input, String encoding)
  73.             throws Exception {
  74.         InputStreamReader insreader = new InputStreamReader(input, encoding);
  75.         BufferedReader bin = new BufferedReader(insreader);
  76.         List<String> lines = new ArrayList<String>();
  77.         String line;
  78.         while ((line = bin.readLine()) != null) {
  79.             lines.add(line);
  80.         }
  81.         bin.close();
  82.         insreader.close();
  83.         return lines;
  84.     }
  85.     /**
  86.      * 以GBK格式将输入流按行置入一个List<String>
  87.      * 
  88.      * @param input
  89.      * @return
  90.      * @throws Exception
  91.      */
  92.     public static List<String> toLines(InputStream input) throws Exception {
  93.         return toLines(input, "GBK");
  94.     }
  95.     /**
  96.      * 转换为每行补充指定换行符(例如:"/n","</br>")
  97.      * 
  98.      * @param input
  99.      * @param lineBreak
  100.      * @return
  101.      * @throws Exception
  102.      */
  103.     public static String toStringWithLineBreak(InputStream input,
  104.             String lineBreak) throws Exception {
  105.         List<String> lines = toLines(input);
  106.         StringBuilder sb = new StringBuilder(20480);
  107.         for (String line : lines) {
  108.             sb.append(line);
  109.             if (lineBreak != null) {
  110.                 sb.append(lineBreak);
  111.             }
  112.         }
  113.         return sb.toString();
  114.     }
  115.     /**
  116.      * 将字符串转出到指定文件
  117.      * @param saveFile
  118.      * @param content
  119.      */
  120.     public static void toFile(File saveFile, String content) {
  121.         File parent = saveFile.getParentFile();
  122.         if (!parent.exists()) {
  123.             parent.mkdirs();
  124.         }
  125.         PrintWriter out = null;
  126.         try {
  127.             out = new PrintWriter(new FileWriter(saveFile));
  128.             out.print(content);
  129.             out.flush();
  130.         } catch (Exception e) {
  131.             e.printStackTrace();
  132.         } finally {
  133.             if (out != null) {
  134.                 out.close();
  135.             }
  136.         }
  137.     }
  138.     /**
  139.      * 将一组文件打zip包
  140.      * 
  141.      * @param srcFiles
  142.      * @param targetFileName
  143.      * @throws IOException
  144.      */
  145.     public static void filesToZip(List<File> srcFiles, String targetFileName)
  146.             throws IOException {
  147.         String fileOutName = targetFileName + ".zip";
  148.         byte[] buf = new byte[1024];
  149.         FileInputStream in = null;
  150.         FileOutputStream fos = null;
  151.         ZipOutputStream out = null;
  152.         try {
  153.             fos = new FileOutputStream(fileOutName);
  154.             out = new ZipOutputStream(fos);
  155.             for (File file : srcFiles) {
  156.                 in = new FileInputStream(file);
  157.                 out.putNextEntry(new ZipEntry(file.getName()));
  158.                 int len;
  159.                 while ((len = in.read(buf)) != -1) {
  160.                     out.write(buf, 0, len);
  161.                 }
  162.                 if (in != null) {
  163.                     in.close();
  164.                 }
  165.             }
  166.         }  捕获  (例外五){
  167.             e.printStackTrace();
  168.         }  最后  {
  169.             if  (in!=  null){
  170.                 附寄();
  171.             }
  172.             if  (fos!=  null){
  173.                 out.closeEntry();
  174.                 out.close();
  175.                 fos.close();
  176.             }
  177.         }
  178.     }
  179.     public  static  void  main(String [] args){
  180.         尝试  {
  181.             文件doc1 =   文件(
  182.                     “E://workspace//test//doc//1272531757100_1.doc”);
  183.             IOUtils.toString(new  FileInputStream(doc1));
  184.         }  捕获  (例外五){
  185.             e.printStackTrace();
  186.         }
  187.     }
  188. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值