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

来源:互联网 发布:wkwebview js交互 oc 编辑:程序博客网 时间:2024/06/05 23:06

博客转自:http://blog.csdn.net/amosryan/article/details/6568783

功能目录:

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

 

源码:

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

0 0
原创粉丝点击