Do not use BufferedReader . You already have an OutputStream in your hands, so just get the InputStream file and connect the bytes to the input to output its normal Java IO path . That way, you also don't have to worry about new lines being eaten by 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) {} } }
Using Reader / Writer will include problems with character encoding , if you do not know / specify the encoding in advance. You also should not know about them here. So just leave it aside.
To improve performance a bit more, you can always wrap InputStream and OutputStream in BufferedInputStream and BufferedOutputStream respectively.
source share