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

  • Home
  • SEARCH
  • 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 9319035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:16:58+00:00 2026-06-19T03:16:58+00:00

hey everyone :] just wondering if is it possible to read and write from

  • 0

hey everyone :] just wondering if is it possible to read and write from a file in different classes without having to close the stream and start over again?

i need to use DataOutputStream and DataInputStream. i currently have 2 classes, my first class are supposed to write all objects to a file. Depending of what kind of object it is. I will be sending the object to a different class where it will continue writing.

the problem is that either i will just make to write 1 object before the stream is closed and i can’t continue writing the next object, or each object will overwrite the last object.

    public int skrivTilFil( String filnavn)
  {
      try{DataOutputStream input = new DataOutputStream(new FileOutputStream(filnavn)))
      {
          Bok løper = første;
          {
              while(løper != null)
              {
                  if(løper instanceof Skolebok)
                  {
                      Skolebok bok = (Skolebok) løper;
                      bok.skrivObjektTilFil(input);
                  }

                  løper = løper.neste;
              }
          }
      }
  }




public void skrivObjektTilFil(DataOutputStream output) // skolebok class
         {
             try
             { 
                            output.writeUTF("Skolefag");
                output.writeInt(klassetrinn);
                output.writeUTF(skolefag);
                super.skrivObjektTilFil(output);
             }catch(IOException e)
             {               
                            System.out.println("General I/O exception: "
                            e.getMessage());e.printStackTrace(); 
                        }
         }

here i will get IOException because the stream is closed, so i can’t continue with the next object, but if i set DataOutputStream in the second class, i will be overwritting each object. is there i simple solution to this problem?

  • 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-06-19T03:16:59+00:00Added an answer on June 19, 2026 at 3:16 am

    You need to append to the FileOutputStream.

    You should modify its creation as in:

    ...                                                                           
    try{DataOutputStream input =                                  //append
                new DataOutputStream(new FileOutputStream(filnavn,true)))
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone. I am having some issues on my end and hopefully it's just
Hey everyone. Just wondering what everyone's thoughts are on playing FLV video on a
Hey everyone, I was just wondering if anyone knows how to create a UITableviewCell
Hey everyone :) I'm having a small issue and i'd love to hear from
Hey everyone, I was just wondering whether anybody had any experience with displaying svg
Hey everyone, just a quick thing, I have the hex to integer working, but
Hey everyone I am having a little problem with my jQuery I am trying
Hey everyone, I just finished up an application I've been working on for a
Hey everyone, I am having a problem with a website I'm trying to build;
Possible Duplicate: Why does simple C code receive segmentation fault? Hey Everyone, I'm sure

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.