一、前言
关于alibaba的dubbo源码包com.alibaba.dubbo.common.utils.IOUtils输入输出io工具类,对InputStream/OutputStream输入输出流、文本Reader/Writer写入, 输出流OutputStream与字符串数组、文件与字符串数组不同方式写出数据处理,详情参见源码说明。
二、源码说明
package com.alibaba.dubbo.common.utils;@b@@b@import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.OutputStreamWriter;@b@import java.io.PrintWriter;@b@import java.io.Reader;@b@import java.io.StringReader;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@public class IOUtils {@b@ private static final int BUFFER_SIZE = 1024 * 8;@b@@b@ private IOUtils() {@b@ }@b@@b@ /**@b@ * write.@b@ *@b@ * @param is InputStream instance.@b@ * @param os OutputStream instance.@b@ * @return count.@b@ * @throws IOException@b@ */@b@ public static long write(InputStream is, OutputStream os) throws IOException {@b@ return write(is, os, BUFFER_SIZE);@b@ }@b@@b@ /**@b@ * write.@b@ *@b@ * @param is InputStream instance.@b@ * @param os OutputStream instance.@b@ * @param bufferSize buffer size.@b@ * @return count.@b@ * @throws IOException@b@ */@b@ public static long write(InputStream is, OutputStream os, int bufferSize) throws IOException {@b@ int read;@b@ long total = 0;@b@ byte[] buff = new byte[bufferSize];@b@ while (is.available() > 0) {@b@ read = is.read(buff, 0, buff.length);@b@ if (read > 0) {@b@ os.write(buff, 0, read);@b@ total += read;@b@ }@b@ }@b@ return total;@b@ }@b@@b@ /**@b@ * read string.@b@ *@b@ * @param reader Reader instance.@b@ * @return String.@b@ * @throws IOException@b@ */@b@ public static String read(Reader reader) throws IOException {@b@ StringWriter writer = new StringWriter();@b@ try {@b@ write(reader, writer);@b@ return writer.getBuffer().toString();@b@ } finally {@b@ writer.close();@b@ }@b@ }@b@@b@ /**@b@ * write string.@b@ *@b@ * @param writer Writer instance.@b@ * @param string String.@b@ * @throws IOException@b@ */@b@ public static long write(Writer writer, String string) throws IOException {@b@ Reader reader = new StringReader(string);@b@ try {@b@ return write(reader, writer);@b@ } finally {@b@ reader.close();@b@ }@b@ }@b@@b@ /**@b@ * write.@b@ *@b@ * @param reader Reader.@b@ * @param writer Writer.@b@ * @return count.@b@ * @throws IOException@b@ */@b@ public static long write(Reader reader, Writer writer) throws IOException {@b@ return write(reader, writer, BUFFER_SIZE);@b@ }@b@@b@ /**@b@ * write.@b@ *@b@ * @param reader Reader.@b@ * @param writer Writer.@b@ * @param bufferSize buffer size.@b@ * @return count.@b@ * @throws IOException@b@ */@b@ public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {@b@ int read;@b@ long total = 0;@b@ char[] buf = new char[BUFFER_SIZE];@b@ while ((read = reader.read(buf)) != -1) {@b@ writer.write(buf, 0, read);@b@ total += read;@b@ }@b@ return total;@b@ }@b@@b@ /**@b@ * read lines.@b@ *@b@ * @param file file.@b@ * @return lines.@b@ * @throws IOException@b@ */@b@ public static String[] readLines(File file) throws IOException {@b@ if (file == null || !file.exists() || !file.canRead())@b@ return new String[0];@b@@b@ return readLines(new FileInputStream(file));@b@ }@b@@b@ /**@b@ * read lines.@b@ *@b@ * @param is input stream.@b@ * @return lines.@b@ * @throws IOException@b@ */@b@ public static String[] readLines(InputStream is) throws IOException {@b@ List<String> lines = new ArrayList<String>();@b@ BufferedReader reader = new BufferedReader(new InputStreamReader(is));@b@ try {@b@ String line;@b@ while ((line = reader.readLine()) != null)@b@ lines.add(line);@b@ return lines.toArray(new String[0]);@b@ } finally {@b@ reader.close();@b@ }@b@ }@b@@b@ /**@b@ * write lines.@b@ *@b@ * @param os output stream.@b@ * @param lines lines.@b@ * @throws IOException@b@ */@b@ public static void writeLines(OutputStream os, String[] lines) throws IOException {@b@ PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));@b@ try {@b@ for (String line : lines)@b@ writer.println(line);@b@ writer.flush();@b@ } finally {@b@ writer.close();@b@ }@b@ }@b@@b@ /**@b@ * write lines.@b@ *@b@ * @param file file.@b@ * @param lines lines.@b@ * @throws IOException@b@ */@b@ public static void writeLines(File file, String[] lines) throws IOException {@b@ if (file == null)@b@ throw new IOException("File is null.");@b@ writeLines(new FileOutputStream(file), lines);@b@ }@b@@b@ /**@b@ * append lines.@b@ *@b@ * @param file file.@b@ * @param lines lines.@b@ * @throws IOException@b@ */@b@ public static void appendLines(File file, String[] lines) throws IOException {@b@ if (file == null)@b@ throw new IOException("File is null.");@b@ writeLines(new FileOutputStream(file, true), lines);@b@ }@b@@b@}