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

简介: [file]IO常用工具类IOUtils(Java读文件、写文件、打Zip包)http://www.bieryun.com/1003.html 功能目录: 将输入流转换成字节流将文件读取为一个字符串以指定编码格式将输入流按行置入一个List<String>以GBK格式将输入流按行置入一个List.

[文件] 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. }
相关文章
|
1天前
|
安全 Java 调度
Java一分钟:多线程编程初步:Thread类与Runnable接口
【5月更文挑战第11天】本文介绍了Java中创建线程的两种方式:继承Thread类和实现Runnable接口,并讨论了多线程编程中的常见问题,如资源浪费、线程安全、死锁和优先级问题,提出了解决策略。示例展示了线程通信的生产者-消费者模型,强调理解和掌握线程操作对编写高效并发程序的重要性。
19 3
|
2天前
|
Java 开发者
Java一分钟之-Java IO流:文件读写基础
【5月更文挑战第10天】本文介绍了Java IO流在文件读写中的应用,包括`FileInputStream`和`FileOutputStream`用于字节流操作,`BufferedReader`和`PrintWriter`用于字符流。通过代码示例展示了如何读取和写入文件,强调了常见问题如未关闭流、文件路径、编码、权限和异常处理,并提供了追加写入与读取的示例。理解这些基础知识和注意事项能帮助开发者编写更可靠的程序。
8 0
|
2天前
|
Java
【JAVA基础篇教学】第五篇:Java面向对象编程:类、对象、继承、多态
【JAVA基础篇教学】第五篇:Java面向对象编程:类、对象、继承、多态
|
2天前
|
Java
java中File转为MultipartFile的问题解决
java中File转为MultipartFile的问题解决
|
2天前
|
存储 安全 Java
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
Java容器类List、ArrayList、Vector及map、HashTable、HashMap
|
3天前
|
Java 编译器 开发者
Java一分钟之-继承:复用与扩展类的特性
【5月更文挑战第9天】本文探讨了Java中的继承机制,通过实例展示了如何使用`extends`创建子类继承父类的属性和方法。文章列举了常见问题和易错点,如构造器调用、方法覆盖、访问权限和类型转换,并提供了解决方案。建议深入理解继承原理,谨慎设计类结构,利用抽象类和接口以提高代码复用和扩展性。正确应用继承能构建更清晰、灵活的代码结构,提升面向对象设计能力。
9 0
|
3天前
|
Java
【Java多线程】面试常考 —— JUC(java.util.concurrent) 的常见类
【Java多线程】面试常考 —— JUC(java.util.concurrent) 的常见类
13 0
|
3天前
|
Java API 调度
【Java多线程】Thread类的基本用法
【Java多线程】Thread类的基本用法
6 0
|
3天前
|
SQL Java 数据库连接
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
9 0
|
4天前
|
Java
Java一分钟之-类与对象:面向对象编程入门
【5月更文挑战第8天】本文为Java面向对象编程的入门指南,介绍了类与对象的基础概念、常见问题及规避策略。文章通过代码示例展示了如何定义类,包括访问修饰符的适当使用、构造器的设计以及方法的封装。同时,讨论了对象创建与使用时可能遇到的内存泄漏、空指针异常和数据不一致等问题,并提供了相应的解决建议。学习OOP需注重理论与实践相结合,不断编写和优化代码。
26 1