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

功能目录:

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

 

源码:

package amosryan.utility.file;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * IO常用工具包
 * @author amosryan
 * @since 2010-06-03
 */
public class IOUtils {

	/**
	 * 将输入流转换成字节流
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static byte[] toBytes(InputStream input) throws Exception {
		byte[] data = null;
		try {
			ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int read = 0;
			while ((read = input.read(b)) > 0) {
				byteOut.write(b, 0, read);
			}
			data = byteOut.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			input.close();
		}
		return data;
	}

	/**
	 * 将文件读取为一个字符串
	 * 
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String toString(File file) throws Exception {
		return toString(new FileInputStream(file));
	}

	/**
	 * 将输入流转换为一个串
	 * 
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String toString(InputStream input) throws Exception {
		return toStringWithLineBreak(input, null);
	}

	/**
	 * 以指定编码格式将输入流按行置入一个List<String>
	 * 
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static List<String> toLines(InputStream input, String encoding)
			throws Exception {
		InputStreamReader insreader = new InputStreamReader(input, encoding);
		BufferedReader bin = new BufferedReader(insreader);
		List<String> lines = new ArrayList<String>();
		String line;
		while ((line = bin.readLine()) != null) {
			lines.add(line);
		}
		bin.close();
		insreader.close();
		return lines;
	}

	/**
	 * 以GBK格式将输入流按行置入一个List<String>
	 * 
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static List<String> toLines(InputStream input) throws Exception {
		return toLines(input, "GBK");
	}

	/**
	 * 转换为每行补充指定换行符(例如:"/n","</br>")
	 * 
	 * @param input
	 * @param lineBreak
	 * @return
	 * @throws Exception
	 */
	public static String toStringWithLineBreak(InputStream input,
			String lineBreak) throws Exception {
		List<String> lines = toLines(input);
		StringBuilder sb = new StringBuilder(20480);
		for (String line : lines) {
			sb.append(line);
			if (lineBreak != null) {
				sb.append(lineBreak);
			}
		}
		return sb.toString();
	}

	/**
	 * 将字符串转出到指定文件
	 * @param saveFile
	 * @param content
	 */
	public static void toFile(File saveFile, String content) {
		File parent = saveFile.getParentFile();
		if (!parent.exists()) {
			parent.mkdirs();
		}
		PrintWriter out = null;
		try {
			out = new PrintWriter(new FileWriter(saveFile));
			out.print(content);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 将一组文件打zip包
	 * 
	 * @param srcFiles
	 * @param targetFileName
	 * @throws IOException
	 */
	public static void filesToZip(List<File> srcFiles, String targetFileName)
			throws IOException {
		String fileOutName = targetFileName + ".zip";
		byte[] buf = new byte[1024];
		FileInputStream in = null;
		FileOutputStream fos = null;
		ZipOutputStream out = null;
		try {
			fos = new FileOutputStream(fileOutName);
			out = new ZipOutputStream(fos);
			for (File file : srcFiles) {
				in = new FileInputStream(file);
				out.putNextEntry(new ZipEntry(file.getName()));
				int len;
				while ((len = in.read(buf)) != -1) {
					out.write(buf, 0, len);
				}
				if (in != null) {
					in.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (fos != null) {
				out.closeEntry();
				out.close();
				fos.close();
			}
		}
	}

	public static void main(String[] args) {
		try {
			File doc1 = new File(
					"E://workspace//test//doc//1272531757100_1.doc");
			IOUtils.toString(new FileInputStream(doc1));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值