File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes How to read and after write a video (or any binary file) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to read and after write a video (or any binary file)" Watch "How to read and after write a video (or any binary file)" New topic

How to read and after write a video (or any binary file)

Daniel Lima

Joined: Feb 12, 2010
Posts: 14
Hello Guys!

I need your help to learn how to do this....

I have a video un a specific file/directory like

I want create a file like this:

After I want put this video in a byte array:

And after I want to write another file:

With the bytes of the first File...

I want to learn how to write a new File with the byte[] information of the first file. I want to learn how to get the byte[] of a video file, and after how to create an other file from byte[].


Jayr Motta
Ranch Hand

Joined: Jul 30, 2010
Posts: 110

Hi Daniel,

As I'm a brazilian colleague, i'll not give you the fish. I'll give you the rod. Right?

You're dealing with the I/O API of Java. If you really a representation of your file as an object to do something, the best approach is the one that you started to think.
If you really just need the content of the file you can do something like this:

Note that the method getResourceAsStream should be used carefully, once the classloader behind it is arbitrary and in an application server or web server can access a completely different folder.

This will return an InputStream that may be used to do whatever you want. You can create an byte array to use as a buffer and put the bytes that you've read there. The method read will return -1 if you reached the end of the stream. So you can iterate over the inputstream filling the buffer with the data read and then writing it to an OutputStream object like a FileOutputStream.

This is useful when you're dealing with some I/O operation through the network once the nature of networking that can become slower and can corrupt the data if you try to send an entire and large bunch of bytes over. But how I see that you're dealing with I/O operations that access the file system directly, you can use the inputstream returned from the snippet above and call available() method, it will return the amount of bytes available in the inputstream. You can use it to create your new byte array with the correct size of the input stream and then read it in just one call of the read method and then pass it to the FileOutputStream.

Note that once your new file doesn't exist yet, you'll really need an instance of File. Then you can pass to the constructor of your new instance of FileOutputStream.

I think with this you'll be able to do what you want, and if there is missing something you can ask again or research by yourself.

Feel free to ask again! Thanks!

Feel free to ask me anything!!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
Daniel Lima

Joined: Feb 12, 2010
Posts: 14
Hey Jayr!!!
Thank you very much!

I'll think about this approach....
If I have any doubt you'll know....
And when I do it you'll know too...

tnx man!
I agree. Here's the link:
subject: How to read and after write a video (or any binary file)
jQuery in Action, 3rd edition