Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Blackberry Api: Writing an array of bytes to a file

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

        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:

        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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Blackberry Api: Writing an array of bytes to a file

    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.

  5. The Following User Says Thank You to Norm For This Useful Post:

    jules0075 (July 18th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Blackberry Api: Writing an array of bytes to a file

    append byte data to the end of a file
    Some class constructors take a boolean flag to specify that data is to be appended.

    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.

Similar Threads

  1. Reading an int from bytes(binary file) - HELP
    By yogiyogi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 02:03 PM
  2. convert vector to array of bytes
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: May 3rd, 2010, 11:00 AM
  3. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  4. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM

Tags for this Thread