public static long copy(Readable from, Appendable to) throws IOException {
checkNotNull(from);
checkNotNull(to);
CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
long total = 0;
while (from.read(buf) != -1) {
buf.flip();
to.append(buf);
total += buf.remaining();
buf.clear();
}
return total;
}
import com.google.common.io.CharStreams;
import ...
/**
* 方法会关闭输入流 in,jdk7以上try 可以自动关闭数据流
* @param in 输入流
* @param charset 字符编码
* @return
* @throws IOException
*/
public static String readIoAsString(InputStream in, String charset) throws IOException {
String res;
try(InputStreamReader source = new InputStreamReader(in, charset);) {
res = CharStreams.toString(source);
}
return res;
}