• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 110
Google Web Toolkit Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Daniel Lima
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic