不要使用BufferedReader.你手头已经有了一个OutputStream,所以只需获取文件的InputStream并从输入中输出字节就可以输出
usual Java IO way.这样你也不必担心BufferedReader会吃掉换行符:
public static void writeFile(OutputStream output, File file) throws IOException {
InputStream input = null;
byte[] buffer = new byte[10240]; // 10KB.
try {
input = new FileInputStream(file);
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
} finally {
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
}
如果您事先不知道/指定编码,则使用读取器/写入器将涉及character encoding problems.你实际上也不需要在这里了解它们.所以就把它放在一边.
为了提高性能,您可以始终将InputStream和OutputStream分别包装在BufferedInputStream和BufferedOutputStream中.