Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:13:53+00:00 2026-05-25T15:13:53+00:00

I wrote a file using Java’s FileChannel class that uses RandomAccessFiles. I wrote objects

  • 0

I wrote a file using Java’s FileChannel class that uses RandomAccessFiles. I wrote objects at various locations in the file. The objects were of variable sizes but all of the same class. I wrote the objects using the following idea :

ByteArrayOutputStream bos= new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(r);
        byte[] recordBytes= bos.toByteArray();

    ByteBuffer rbb= ByteBuffer.wrap(recordBytes);

    while(rbb.hasRemaining()) {
        fileChannel.write(rbb);
    }

Now I want to read from such a file. I dont want to have to specify the number of bytes to read. I want to be able to read the object directly using Object Input Stream. How to achieve this ?

I have to use Random Access Files because I need to write to different positions in file. I am also recording in a separate data structure, the locations where objects have been written.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:13:54+00:00Added an answer on May 25, 2026 at 3:13 pm

    I have to use Random Access Files because I need to write to different
    positions in file.

    No, you don’t. You can reposition a FileOutputStream or FileInputStream via its channel.

    That would significantly simplify your writing code as well: you wouldn’t need to use the buffer or channel, and depending on your needs you could omit the ByteArrayOutputStream as well. However, as you note in a comment, you won’t know the size of the object in advance, and the ByteArrayOutputStream is a useful way to verify that you don’t overrun your allotted space.

    Object obj = // something
    
    FileOutputStream fos = // an initialized stream
    
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    oos.flush();
    
    if (bos.size() > MAX_ALLOWED_SIZE)
       throw // or log, or whatever you want to do
    else
    {
        fos.getChannel().position(writeLocation);
        bos.writeTo(fos);
    }
    

    To read the objects, do the following:

    FileInputStream fis = // an initialized stream
    
    fis.getChannel().position(offsetOfSerializedObject);
    ObjectInputStream iis = new ObjectInputStream(new BufferedInputStream(fis));
    Object obj = iis.readObject();
    

    One comment here: I wrapped the FileInputStream in a BufferedInputStream. In this specific case, where the file stream is repositioned before each use, that can provide a performance benefit. Be aware, however, that the buffered stream can read more bytes than are needed, and there are some situations using construct-as-needed object streams where it would be a really bad idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This Java web-app I am using that other developers wrote is structured in the
I wrote a java program that using the SVNKit. The timestamp of checked out
I have to write an XML file using java.The contents should be written from
I'm using Netbeans to write Scala and Java. Netbeans generated a .jar file for
I've wrote a java code and compiled it. (foo1.6.class) According to my search, my
I have a bunch of diagrams created using a Java diagramming tool that I
I wrote a Java application that reads and sends SMS messages from a USB
I'm writing a program that uses File I/O to traverse through a directory given
Possible Duplicate: How can I lock a file using java (if possible) I have
I'm using Grails and I have a local-plugin (that I wrote) that I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.