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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:28:21+00:00 2026-06-01T18:28:21+00:00

i am wondering if there is anyway to check if a ObjectInputStream or ObjectOutputStream

  • 0

i am wondering if there is anyway to check if a ObjectInputStream or ObjectOutputStream is empty or not. i mean, in my program. at first time when it runs, the ObjectInputStream will use its readObject() method then because still the file empty it gives me an EOF exception( end of file) so i would like to check if it is empty or not then get rid off the exception:

And am i doing it right? for Serializing, i made the class with same name and attribute as below in both client and server.

public class KeyAdr implements Serializable{

 String adr;
 String key;


}
....

    static FileInputStream fIn=null;
    static ObjectInputStream oIn=null;
    private static KeyAdr test=new KeyAdr();

....

           fIn= new FileInputStream("d:\\someFile.ser");
           oIn = new ObjectInputStream(fIn);
           test= (KeyAdr) oIn.readObject(); 

EDIT:

 static  File serAdrKey=new File("d:\\someFile.ser");
     static    ObjectOutputStream oOut;
     static    FileOutputStream fOut;
     static  final   Pattern WebUrlPattern = Pattern.compile (WebUrlRegex);
     private static String WebUrlStr;
     static KeyAdr letsDoIt= new KeyAdr();

....

 public static void openStreams() throws IOException
        {
         fOut= new FileOutputStream(serAdrKey);
     oOut = new ObjectOutputStream(fOut);

        }


        @Override
public void    beforeWindowOpen(NavigationEvent event) 
        {

                     temp=event.getURL().toString();


  Matcher WebUrlMatcher = WebUrlPattern.matcher (temp);
    if (WebUrlMatcher.matches ())
    {
        int n = WebUrlMatcher.groupCount ();
      for (int i = 0; i <= n; ++i) {
    WebUrlStr = WebUrlMatcher.group (i);

}

                    letsDoIt.adr=WebUrlStr;    

                    try {
                    oOut.writeObject(letsDoIt);

                } catch (IOException ex) {
                    Logger.getLogger(Cobratest2.class.getName()).log(Level.SEVERE, null, ex);
                }


                   try {
                oOut.flush();


                   } catch (IOException ex) {
                Logger.getLogger(Cobratest2.class.getName()).log(Level.SEVERE, null, ex);
            }

Edit 2

fIn= new FileInputStream("d:\\someFile.ser");
PushbackInputStream input = new PushbackInputStream(fIn);
int c = input.read();
if(c != -1)
{
  input.unread(c);
  oIn = new ObjectInputStream(input);
  test = (KeyAdr) oIn.readObject();
  // ......
}

Edit 3:

the Edit2 codes gave me following exception with the stacktrace:

Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at test.Test.processClient(Test.java:117)
            at test.Test.run(Test.java:92)
            at test.Test.main(Test.java:159)
  • 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-01T18:28:23+00:00Added an answer on June 1, 2026 at 6:28 pm

    You may want to use the read method first to check if there is at least 1 byte.

    read() is guaranteed to return -1 when the end of the stream has been reached, if the very first read() returns -1 the file must be empty

    Addendum:
    Using the FileInputStream you should be able to mark/reset to avoid losing the read byte.

    You may however want to use java.util.File to check emptiness of the file beforehand.

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

Sidebar

Related Questions

I was wondering if there was anyway to use functions like ReadFileEx that require
I was wondering if there is anyway to check if the up arrow or
Hey! I was wondering if there was any way to check if the first
I am using python unit test module. I am wondering is there anyway to
I was wondering if there's anyway to filter a set using NSPredicate sorted by
I was wondering if there is any way to check if an object is
I was wondering if there was a way to check if a tag has
I was just wondering is there anyway to stop application being terminated at all
I was wondering if is there any way to quickly check iOS/objective-c snippets or
Just wondering if there is any way to check if the value of a

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.