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 6549553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:06:25+00:00 2026-05-25T12:06:25+00:00

import java.io. * ; public class Ser { public static void main(String args[]) {

  • 0
import java.io. * ;
public class Ser {

    public static void main(String args[]) {

        try {
            John myObj = new John("Sachin", "Cricket");
            System.out.println(myObj);
            FileOutputStream fos = new FileOutputStream("FileName");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(myObj);
            oos.flush();
            oos.close();
        } catch (Exception e) {
            System.out.println("Expection" + e);
            System.exit(0);
        }

        try {
            John myObj2;
            FileInputStream fis = new FileInputStream("FileName");
            ObjectInputStream ois = new ObjectInputStream(fis);
            myObj2 = (John) ois.readObject();
            ois.close();
            System.out.println("New Object" + myObj2);
        } catch (Exception e) {
            System.out.println("Expection" + e);
            System.exit(0);
        }

    }
}

class John implements Serializable {

    private String name;
    private String department;

    public John(String name, String department) {
        this.name = name;
        this.department = department;
    }

    public String toString() {
        return "Name" + name + " " + "Department" + department;

    }

}

I have a few questions in the above example.

  1. When to use flush method and why do we use it?
  2. What does close method carry a score here?
  3. myObj2 = (John) ois.readObject(); … please correct me if i am wrong, i am reading the file object and storing into another object and typecasting the file object.
  4. What are the alternatives of Serialization or persisting the data in Java. I don’t want the data to get into file as byte stream.
  • 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-25T12:06:25+00:00Added an answer on May 25, 2026 at 12:06 pm

    When to use flush method and why do we use it?

    It flushes anything which is still buffered by the OutputStream. A detailed description is available in JavaDoc.


    What does close method carry a score here?

    I’m not sure what you mean by ‘carry a score’, but the close method finally closes resources (Input or Output), it releases e.g. file handles. You should always call close in a finally block to clean up all references the OS may have.

    InputStream is = null;
    try {
        is = new FileInputStream(...);
        // do stuff
    } catch (IOException e) {
        // do stuff
    } finally {
        if (is != null) {
            is.close();
        }
    }
    

    myObj2 = (John) ois.readObject(); … please correct me if i am wrong,
    i am reading the file object and storing into another object and
    typecasting the file object.

    Somehow correct, you deserialize an Object writen to the file before. Those files are proprietary and can only be understood by Java, so you last question is a good one!


    What are the alternatives of Serialization or persisting the data in
    Java. I don’t want the data to get into file as byte stream.

    You have a number of options. For client application you may want to use XML, JSON, or a lightweight database like SQLlite. On server-side you may have a look at more robust databases (e.g. MySQL) as well.

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

Sidebar

Related Questions

import java.io.*; public class Demo{ public static void main(String[] args){ File f = new
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.io.*; public class TerminateWhen { public static void main(String args[]) throws IOException {
import java.util.scanner; import javax.swing.JOptionPane; public class FirstHomeJavaApplet{ public static void main(String[] args){ int num1=2;
import java.lang.reflect.Method; public class ClassLoadingTester { public static void main(String a[]){ try{ ClassLoader loader=new
import java.io.*; public class Mainclassexec { public static void main(String[] args) { String input
import java.io.*; public class ArrayApp{ public static void main(String[] args){ System.out.println(lllll); } // end
import java.util.Scanner; public class Mal { public static void main(String[] args) { System.out.println(Welcome); Scanner
import java.util.Scanner ; public class Mal { public static void main (String []args) {

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.