This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

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

Daniel Lima
Greenhorn

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[].

Thanks!

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!
www.BlackBeltFactory.com/ui#!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
Daniel Lima
Greenhorn

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!
 
Don't get me started about those stupid light bulbs.
 
subject: How to read and after write a video (or any binary file)
 
Similar Threads
OutputStream System.out to String BLOB
How to encode image in parts?
Combining two files to create a new file and then reading it...
Writing non text file
Image transfer through sockets