Here is an example
public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("c:/data.txt"); FileOutputStream outputStream = new FileOutputStream("c:/result.txt"); byte[] buffer = new byte[1000]; while (inputStream.available() > 0){ int count = inputStream.read(buffer); outputStream.write(buffer, 0, count); } inputStream.close(); outputStream.close(); } Here are a few questions.
Why do I need to specify
outputStream.write(buffer, 0, count);the number of bytes to write (from and to0, count), if theread(byte[] byte)method still reads bytes until the buffer is full or the bytes in the file run out.I understand that how many bytes were given so much and wrote
outputStream.write(buffer);Why put it inside out?- Why specify a buffer when
BufferedInputStream()is present? If I understand correctly, it will work as well as if we specifybyte[] byte = new byte[1000]... It will also buffer bytes before writing ....
EDIT
Concerning my second question, here is a method for an example.
private void writeFile(File file, byte[] bytes) { BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(file)); bos.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (image != null) { image.close(); } if (null != bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } EDIT2

buffer? - post_zeewBufferedOutputStreamdoes not have awritemethod with such a signature. Completed the answer - selya