Blackberry Api: Writing an array of bytes to a file
I am trying to read and write metadata to/from an mp3 file. I can read the data, but I cannot figure out how to write it.
I use the following code to get file access:
Code :
FileConnection file = (FileConnection) Connector.open("file:///store/home/user/music/song.mp3");
if(file.exists())
java.io.InputStream inputStream = file.openInputStream();
Later, I read the data using the following code:
Code :
buffer = new byte[length]; // length is predetermined earlier
if (inputStream.read(buffer, 0, length) == length)
String info = new String((buffer));
How do I write data (bytes) to the a designated location in the file? I am unsure of both the IO declarations and the specific code required to output my bytes.
Thank you in advance,
Julia
PS: I'm really new to this, I would really appreciate detailed answers. Thank you!
Re: Blackberry Api: Writing an array of bytes to a file
What classes and packages do you have for writing to files?
Are they similiar to the Java API classes and packages?
Re: Blackberry Api: Writing an array of bytes to a file
The two classes that I know of are OutputStream and InputStream.
I found this website that contains code to write data (bytes) to a new file. But I need to stick my data in the middle of a file, or at least at the very beginning/end. Is this possible?
Thank you!
Re: Blackberry Api: Writing an array of bytes to a file
Quote:
need to stick my data in the middle of a file, or at least at the very beginning/end.
Think of a file as a string of words typed on a piece of paper. To add words at the beginning, means that all the words that were at the beginning have to be moved over to the right. The same when inserting in the middle.
Bottom line - everything from your insert point to the end of the file must be rewritten.
Re: Blackberry Api: Writing an array of bytes to a file
To insert in beginning/middle:
If I understand you correctly, I will need to create a separate class that will read in the data and then re-write everything past the point where I inserted data? And there is no internal class that does that already.
To insert at the end:
Is there an easy way to append byte data to the end of a file?
Thanks again!
Re: Blackberry Api: Writing an array of bytes to a file
Quote:
append byte data to the end of a file
Some class constructors take a boolean flag to specify that data is to be appended.
Quote:
I will need to create a separate class that will read in the data and then re-write everything past the point
Something like that. Some code some where has to read and rewrite the data that is being overwritten by the insert.