文件处理程序中的文件结束错误

时间:2014-06-18 14:17:27

标签: java file-io

我正在使用文件处理在java中创建一个数据库项目。该程序正在运行而不编译错误。以下几点无效

  1. 文件仅存储第一条记录。 (如果程序再次运行则覆盖文件)
  2. 我想显示所有记录,但唯一的第一条记录显示为Exeception
  3. 请提供建议..

    import java.io.*;
    
    public class Student implements Serializable
    {
        private int roll;                       
        private String name;                    //To store name of Student
        private int[] marks = new int[5];       //To store marks in 5 Subjects
        private double percentage;              //To store percentage
        private char grade;                     //To store grade
        public Student()
        {
            roll = 0;
            name = "";
            for(int i = 0 ; i < 5 ; i++)
                marks[i] = 0;
            percentage = 0;
            grade = ' ';
        }
        public Student(int roll , String name ,int[] marks)
        {
            setData(roll,name,marks);
        }
        public void setData(int roll , String name ,int[] marks)
        {
            this.roll = roll;
            this.name = name;
            for(int i = 0 ; i < 5 ; i++)
                this.marks[i] = marks[i];
            cal();
        }
        //Function to calculate Percentage and Grade
        private void cal()
        {
            int sum = 0;
            for(int i = 0 ; i < 5 ;i++)
                sum = sum + marks[i];
            percentage = sum/5;
    
            if(percentage>85)
                grade = 'A';
            else if(percentage > 70)
                grade = 'B';
            else if (percentage >55)
                grade = 'C';
            else if (percentage > 33)
                grade = 'E';
            else
                grade = 'F';
        }
        public char getGrade() { return grade; }
        public double getPercentage() { return percentage; }
        @Override
        public String toString()
        {
            string format = "Roll Number : %4d, Name : -%15s "
                          + "Percentage : %4.1f Grade : %3s";
            return String.format(format, roll, name, percentage, grade);
        }
    }
    

    第二档

    import java.io.*;
    
    public class FileOperation
    {
        public static void writeRecord(ObjectOutputStream  outFile, Student temp) 
            throws IOException, ClassNotFoundException
        {
            outFile.writeObject(temp);
            outFile.flush();
        }
    
        public static void showAllRecords(ObjectInputStream inFile) 
            throws IOException, ClassNotFoundException
        {
            Student temp = new Student();
            while(inFile.readObject() != null)
            {
                temp = (Student)inFile.readObject();
                System.out.println(temp);
            }
        }
    }
    

    主文件 (只有两个选项正在运作)

    import java.util.*;
    import java.io.*;
    
    public class Project
    {
        static public void main(String[] args)
            throws IOException,ClassNotFoundException
        {
            ObjectInputStream  inFile;
            ObjectOutputStream outFile;
    
            outFile = new ObjectOutputStream(new FileOutputStream("info.dat"));
            inFile = new ObjectInputStream(new FileInputStream("info.dat"));
    
            Scanner var = new Scanner(System.in) ;
            int roll;
            String name;
            int[] marks = new int[5];
            int chc = 0;
    
            Student s = new Student();
    
            while(chc != 6)
            {
                System.out.print("\t\tMENU\n\n");
                System.out.print("1.Add New Record\n");
                System.out.print("2.View All Records\n");
                System.out.print("3.Search a Record (via Roll Number) \n");
                System.out.print("4.Delete a Record (via Roll Number) \n");
                System.out.print("5.Search a Record (via Record Number)\n");
                System.out.print("6.Exit\n");
                System.out.print("Enter your choice : ");
                chc = var.nextInt();
    
                switch(chc)
                {
                case 1: 
                    System.out.print("\nEnter Roll number of Student : ");
                    roll = var.nextInt();
                    System.out.print("\nEnter Name of Student : ");
                    name = var.next();
                    System.out.println("\nEnter marks in 5 subjects \n");
                    for(int i = 0 ; i < 5 ; i++)
                    {
                        System.out.print("Enter marks in subject " + (i+1) + " ");
                        marks[i] = var.nextInt();
                    }
                    s.setData(roll , name , marks );
                    System.out.println("\n Adding Record to file \n");
                    System.out.printf("Record \n " + s);
                    System.out.println("\n\n");
                    FileOperation.writeRecord(outFile,s);
                    System.out.println("Record Added to File\n ");
                    break;
    
                case 2:
                    System.out.println("All records in File \n");
                    FileOperation.showAllRecords(inFile);
                    break;
    
                default: System.out.println("Wrong choice ");
                }
            }
            outFile.close();
            inFile.close();
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您的程序找到了ClassNotFoundException,您的方法只会抛出IOException

我会检查你的import语句,以确保你引入了ObjectInputStream类

import java.io.ObjectInputStream;

如果你想摆脱未报告的异常,请尝试:

尝试

public static void showAllRecords(ObjectInputStream inFile) throws IOException, ClassNotFoundException