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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:22:22+00:00 2026-06-02T23:22:22+00:00

This is a class I just wrote. Previously it actually created a file in

  • 0

This is a class I just wrote. Previously it actually created a file in another application. But somehow it is not working. It is not creating a new file and I am getting this error:

package hostelmanagement;

/**
 *
 * @author princess
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;

/**
 *
 * @author princess
 */
public class Student implements Serializable, Externalizable  {

public static Set<Student> listOfStudents = new HashSet<Student>();    
public static File oFile = new File("Student.dat"); 


//Data Members
private String studentID;
private String name;
private Date dateOfReg;

//Constructor 
Student(String number,String name)
{
this.studentID = number;
this.name = name;
dateOfReg = new Date(); 
}


public String getName()
{
return name;
}

public String getStudentID()
{
return studentID;
}

public Date getDateOfReg()
{
return dateOfReg;
}

public void register() throws FileNotFoundException, IOException, ClassNotFoundException    
{    

HashSet<Student> sss = Student.getListOfStudents();
sss.add(this);
FileOutputStream OFileStream = new FileOutputStream(oFile);    
ObjectOutputStream ObjectOFileStream = new ObjectOutputStream(OFileStream);    
ObjectOFileStream.writeObject(sss);
ObjectOFileStream.close();      
}

public static HashSet<Student> getListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet ss;
File iFile = new File("Student.dat");
FileInputStream IFileStream = new FileInputStream(iFile);
 ObjectInputStream ObjectIFileStream = new ObjectInputStream(IFileStream);
ss = (HashSet<Student>) ObjectIFileStream.readObject();
ObjectIFileStream.close();
return  (HashSet<Student>) ss;
}

public static void printListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet<Student> sa = Student.getListOfStudents();
for (Student x : sa)
{System.out.println(x.toString());}
}


public static Student getStudentByID(String aNumber) throws FileNotFoundException, IOException, ClassNotFoundException
{ 
HashSet<Student> currentListOfStudents = Student.getListOfStudents();    
Student result = null;   
for (Student x : currentListOfStudents) 
    {   
if (x.getStudentID().equalsIgnoreCase(aNumber))  
        { result = x;
             break;
        }
    }  
      if (result == null)
    {
        JOptionPane.showMessageDialog(null, "Student not found");
    }
    return result; 
}



    @Override
public String toString()
{
// include the code to retrieve assigned apartment    
return "Name: " + name +" StudentID: "+ studentID + " Registered On: " + dateOfReg;
}


    @Override
public boolean equals(Object another)
{
Student stud = (Student)another;
return this.name.equals(stud.name)&& this.studentID.equals(stud.studentID);
}

    @Override
public int hashCode()
{
int hash = name.hashCode();
return hash;

}

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

Error:

run:
Test 1
Apr 27, 2012 10:19:30 AM hostelmanagement.HostelManagement main
SEVERE: null
java.io.FileNotFoundException: Student.dat (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at hostelmanagement.Student.getListOfStudents(Student.java:86)
    at hostelmanagement.Student.register(Student.java:74)
    at hostelmanagement.HostelManagement.main(HostelManagement.java:34)
Exception in thread "main" java.io.FileNotFoundException: Student.dat (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at hostelmanagement.Student.getListOfStudents(Student.java:86)
    at hostelmanagement.HostelManagement.main(HostelManagement.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Where is the 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-02T23:22:24+00:00Added an answer on June 2, 2026 at 11:22 pm

    You error indicates you are trying to read a file which is not there. It will only create new files when you attempt to write to them.

    What I would do is the following.

    public static Set<Student> getListOfStudents() throws IOException, ClassNotFoundException {
        File studentFile = new File("Student.dat");
        FileInputStream in = null;
        try {
            in = new FileInputStream(studentFile);
            ObjectInputStream oos = new ObjectInputStream(in);
            retyurn (Set<Student>) oos.readObject();
        } catch(FileNotFoundException noStudents) {
            return new HashSet<Student>();
        } finally {
            if (in != null)
               try {
                   in.close();
               } catch(IOException ignored) {}
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Essentially, I am just creating two classes where one class, in this case class
This is just an example, but given the following model: class Foo(models.model): bar =
In fact I have this working, just not correctly. I have a callback (posted
Sorry for my bad English. Just like what I asked. I have this class
I've just seen this case class in the Scala actors package: case class !
I'm just wondering .. I have and object like this public class Entry{ public
I just wanted to know if I can point to class using this implementation:
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
This is a MVC 3 project. Just for testing, I have public class MyRoleProvider
I just wanted to confirm the difference here, take this as an example: class

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.