A
andrewzzz
hi guys,
what is the best way to write a byte array to a file?
thanks a lot,bye
what is the best way to write a byte array to a file?
thanks a lot,bye
andrewzzz said:hi guys,
what is the best way to write a byte array to a file?
Paul said:public void writeFile(byte[] data, String fileName) throws IOException{
OutputStream out = new FileOutputStream(fileName); try {
out.write(data); } finally {
out.close(); }
}
andrewzzz said:hi guys,
what is the best way to write a byte array to a file?
thanks a lot,bye
M.J. Dance said:InputStream in = new FileInputStream(file);
byte[] bytes = new byte[file.length()];
in.read(bytes);
in.close();
or, if the going gets extremely tough,
http://www.justfuckinggoogleit.com/search?q=java+file+io+tutorial
M.J. Dance said:byte[] bytes = new byte[file.length()];
in.read(bytes);
Chris said:M.J. Dance said:byte[] bytes = new byte[file.length()];
in.read(bytes);
/NEVER/ do that. Never !
M.J. Dance said:byte[] bytes = new byte[file.length()];
in.read(bytes);
Thomas Fritsch said:It should be replaced by something like this:
byte[] bytes = new byte[file.length()];
for (int n = 0, x; n < bytes.length; n += x ) {
x = in.read(bytes, n, bytes.length - n);
if (x < 0)
throw new EOFException("stream shorter than expected");
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.