This is not true, but appears to be a common confusion.
You only need to call flush() if you need output to get to the disk at some
defined point, prior to closing the output stream. You very, very rarely
need to do this. In all my years programming in Java, I bet I could count on
the fingers of one hand how many times I've needed to call flush().
Here's the canonical code for copying a file. I'll show you where to put
your little data manipulation.
private static final int BUFFER_SIZE = 1024;
...
byte[] buffer = new byte[BUFFER_SIZE];
InputStream inputStream = urlConnection.getInputStream();
try {
// Do not open the output file until you have successfully opened the
input!
FileOutputStream fileOutput = new FileOutputStream(filepath);
try {
int read;
while ((read = inputStream.read(buffer)) >= 0) {
// manipulate the data in the buffer here.
fileOutput.write(buffer, 0, read);
}
} finally {
fileOutput.close();
}
} finally {
inputStream.close();
}
You will see people make this unnecessarily complicated, e.g.
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(...);
...
} finally {
if (fileOut != null) {
fileOut.close();
}
}
Ignore them. They don't know what they're doing. This includes the Android
documentation for FileOutputStream! Fortunately, it's not wrong, just silly.
The extra complexity buys you nothing, but does make it easier to make a
mistake.
However -- it appears that you may be intending this to be a data encryption
technique? Why not use CipherOutputStream, and some real encryption?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en