This program demonstrates how to write a byte array to a file in Java. This task can be performed using FileOutputStream and using some libraries mentioned in this article. The Class FileOutputStream in Java is an output stream used to write data or stream of bytes to a file. The constructor FileOutputStream File file creates a file output stream to write to the file represented by the File object file , which we have created in the code below.
The variable s of type String is passed to the getBytes method, which converts the string into a sequence of bytes and returns an array of bytes. The write method takes the byte array as an argument and writes b. Here can sometimes happen, that you will not read whole file. Such situation can occur only if file is changing while you are reading it. In all other cases IOException is thrown. I could imagine other sources for only reading a portion of the file File is on a network share Remember that RandomAccessFile is not thread safe.
So, synchronization may be needed in some cases. DmitryMitskevich There are other cases as well, on filesystems that are possibly non-conformat. Show 1 more comment. You also need to treat the IOException outside the function. The statement "ios. For the second example to work, I think the read has to be inside a while loop that checks the result for -1 end of file reached.
Sorry, dismiss my remark above, missed the statement setting buffer to length of file. Still, I like the first example way more. Reading a whole file into a buffer in one go is not scalable. You will risk sunning out of memory when the file is large. The "simplest" way would make use of try-with-resources. Cool, but a little to verbose. Palec 11k 7 7 gold badges 56 56 silver badges bronze badges. Tom Tom 41k 28 28 gold badges silver badges bronze badges.
Oleksandr Pyrohov Jeffrey Blattman Jeffrey Blattman Thank you. This is what I need — Nguyen Minh Hien. Amit Amit 1 1 gold badge 6 6 silver badges 11 11 bronze badges.
Unfortunately MappedByteBuffer isn't automatically released. I agree.. Its the default stuff that eclipse puts in. I think I should rethrow the exception! Ive been benchmarking nio in order to create a byte[] from a File.
Other than using a direct buffer it does indeed take twice as much memory. Though it is faster for very large files about twice as fast as a buffered IO for M it seems to lose out by a factor of 5 for files around 5M.
Cuga Cuga 17k 29 29 gold badges silver badges bronze badges. Also, put numRead inside the loop. Declare variables in the smallest valid scope you can. Putting it outside the while loop is only necessary to enable that complicated "while" test; it would be better to do the test for EOF inside the loop and throw an EOFException if it occurs.
Is there also any example about it? Sudip Bhandari Sudip Bhandari 1, 1 1 gold badge 22 22 silver badges 23 23 bronze badges. There are simpler ways, such as the one-liners that have already been mentioned. Such as Android. Simplest Way for reading bytes from file import java. Muhammad Sadiq Muhammad Sadiq 4 4 silver badges 10 10 bronze badges. I argue on being the "Simplest Way" : — BlondCode.
Can you explain here? Why do you have an argue? Maybe it was couple of years ago, but world is changing. I would not label my own solutions with such a statement.
MuhammadSadiq: don't import anything. It has several advantages: It covers the corner case where files report a length of 0 but still have content It's highly optimized, you get a OutOfMemoryException if trying to read in a big file before even trying to load the file. Through clever use of file. Files; import java. BlondCode BlondCode 3, 1 1 gold badge 17 17 silver badges 17 17 bronze badges. What JDK level is this? I belive this is the easiest way: org. Cristian Tetic Cristian Tetic 2 2 silver badges 2 2 bronze badges.
Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 09 Mar, Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
0コメント