Writing objects to random file

Discussion in 'Java' started by Ron Albright, Feb 5, 2004.

  1. Ron Albright

    Ron Albright Guest

    You use a ByteArrayOutputStream but only way you can access the result
    is by getting a copy of the byte array each time you write. Is there a
    better way to do this that doesn't require creating a new byte array
    for each write? I looked at the nio package and didn't see any support
    for writing objects to a file.
    --
    My email isn't airmail.
    Ron
    Ron Albright, Feb 5, 2004
    #1
    1. Advertising

  2. Ron Albright wrote:
    > You use a ByteArrayOutputStream but only way you can access the result
    > is by getting a copy of the byte array each time you write. Is there a
    > better way to do this that doesn't require creating a new byte array
    > for each write?


    'Better'? In what way is the existing situation 'worse'?

    [ I do not know much about the specific area
    that you are speaking of, but wonder if you
    are not attempting to optimize something
    that does not require optimization?]

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Feb 5, 2004
    #2
    1. Advertising

  3. Ron Albright

    Andrew Hobbs Guest

    "Ron Albright" <> wrote in message
    news:...
    > You use a ByteArrayOutputStream but only way you can access the result
    > is by getting a copy of the byte array each time you write. Is there a
    > better way to do this that doesn't require creating a new byte array
    > for each write?


    In what way are you using the ByteArrayOutputStream. You don't need to
    create an array each time you write. Surely that is the objective of using
    this class.

    >I looked at the nio package and didn't see any support
    > for writing objects to a file.


    And how does the above relate to writing to a file. If you want to write
    your bytes to a file then have a look at the java.io package. You need to
    include something like a DataOutputStream and a FileWriter.

    Cheers

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************


    > --
    > My email isn't airmail.
    > Ron
    Andrew Hobbs, Feb 5, 2004
    #3
  4. Ron Albright

    Ron Albright Guest

    On Thu, 5 Feb 2004 11:24:04 +0800, "Andrew Hobbs"
    <> wrote:

    >
    >"Ron Albright" <> wrote in message
    >news:...
    >> You use a ByteArrayOutputStream but only way you can access the result
    >> is by getting a copy of the byte array each time you write. Is there a
    >> better way to do this that doesn't require creating a new byte array
    >> for each write?

    >
    >In what way are you using the ByteArrayOutputStream. You don't need to
    >create an array each time you write. Surely that is the objective of using
    >this class.


    The only way to write the byte array from a
    ByteArrayOutputStream is the getByteArray() method or the
    writeTo() method. The first gives you a copy of the array
    which has to be alloced and garbage collected every time you
    write an object. The second requires opening a stream and
    skipping to the position you want to write to every time you
    write an object. Neither seems very efficient.

    >
    >>I looked at the nio package and didn't see any support
    >> for writing objects to a file.

    >
    >And how does the above relate to writing to a file. If you want to write
    >your bytes to a file then have a look at the java.io package. You need to
    >include something like a DataOutputStream and a FileWriter.


    You can't write to a random position with streams.

    The context of the questions is a fast guarantied persistent
    queue or stack. The idea is a collector getting data and
    persisting it before acking and a distributor passing it on
    and getting an ack before it is removed.

    Ron Albright
    moron(at)KILLSPAM.pobox.com
    Ron Albright, Feb 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,401
    HNguyen
    Dec 21, 2004
  2. Kevin
    Replies:
    19
    Views:
    1,131
    Tris Orendorff
    Feb 13, 2006
  3. 7stud
    Replies:
    11
    Views:
    689
    Dennis Lee Bieber
    Mar 20, 2007
  4. globalrev
    Replies:
    4
    Views:
    756
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,159
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page