
不要使用
BufferedReader。您已经
OutputStream手头上了,因此只需获取
InputStream文件的一个,然后通过通常的Java
IO方式将输入中的字节通过管道传输到输出即可。这样,您也不必担心换行符被以下内容吃掉
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) {} }}如果您不事先知道/指定编码,则使用
Reader/
Writer将涉及字符编码问题。您实际上也不需要在这里了解它们。因此,将其放在一边。
为了进一步提高性能,您可以始终将和分别包装在
InputStream和
OutputStream中。
BufferedInputStream``BufferedOutputStream
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)