从二进制文件中读取数据

时间:2017-02-18 15:38:04

标签: java file-handling

您好我从我创建的文件中读取整个对象时遇到问题。任何人都可以尝试测试我的代码...我是java的初学者。

Students.java

 package chapter5.files;
import java.io.*;
import java.util.*;

public class Students 
{

    /*public static void main(String[] args) {
        // TODO Auto-generated method stub

    }*/
    String name, address;
    int roll, age;
    float phy, chem, comp, math, bio, eng, nep;

    Scanner input = new Scanner(System.in);

    public void data_input()
    {
        System.out.println("Enter the roll no. of the student: ");
        roll=input.nextInt();
        System.out.println("Enter the name of the student: ");
        name=input.nextLine();
        System.out.println("Enter the address of the student: ");
        address=input.nextLine();
        System.out.println("Enter the age of the student: ");
        age=input.nextInt();
        System.out.println("Enter the marks in Physics: ");
        phy=input.nextFloat();
        System.out.println("Enter the marks in Chemistry: ");
        chem=input.nextFloat();
        System.out.println("Enter the marks in Computer: ");
        comp=input.nextFloat();
        System.out.println("Enter the marks in Mathematics: ");
        math=input.nextFloat();
        System.out.println("Enter the marks in Biology: ");
        bio=input.nextFloat();
        System.out.println("Enter the marks in English: ");
        eng=input.nextFloat();
    }
    public float calc_percent()
    {
        float total=phy+chem+comp+math+bio+eng;
        float percent=total/6;
        return percent;
    }
    public void show_individual()
    {
        System.out.println("Roll: " + roll);
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Age: " + age);
        System.out.println("Physics: " + phy);
        System.out.println("Chemistry: " + chem);
        System.out.println("Computer: " + comp);
        System.out.println("Mathematics: " + math);
        System.out.println("Biology: " + bio);
        System.out.println("English: " + eng);
        System.out.println("Percentage: " + calc_percent());
        System.out.println(" ");


    }
}

StudentsMain.java

package chapter5.files;
import java.util.*;
import java.io.*;

public class StudentsMain extends Students 
{
    //****************************************Entry of new data*************************************************//
    public static void entry() throws IOException
    {
        Students s = new Students();
        s.data_input();

        File f = new File("index.dat");
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream foos = new ObjectOutputStream(fos);

        foos.writeObject(s);

        System.out.println("The data has been stored into the file.");

        foos.close();
        fos.close();
        //?????????????????????Why isn't file f closing??????????????????????????????
    }

    //****************************************Show individual data*************************************************//
    public static void show_individual(int temproll) throws IOException, ClassNotFoundException
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the roll no. of the student you want to display the data of: ");
        temproll=input.nextInt();

        File f1 = new File("index.dat");
        FileInputStream fis = new FileInputStream(f1);
        ObjectInputStream fois=new ObjectInputStream(fis);

        while(true)
        {
            Students temp[] = (Students[])is.readObject();//reading in stream..so we have to type cast it in stream.

            for(int i=0;i < temp.length;i++)
            {
                if(temproll==temp[i].roll)
                {
                    temp[i].show_individual();
                }
            }
        }
        fis.close();
        fois.close();
    }

    private static void display_tabular() throws IOException 
    {

        /*File f1 = new File("index.dat");
        FileInputStream fis = new FileInputStream(f1);
        ObjectInputStream fois=new ObjectInputStream(fis);

        while(true)
        {
            Students temp[] = (Students[])is.readObject();//reading in stream..so we have to type cast it in stream.

            for(int i=0;i < temp.length;i++)
            {

            }
        }*/
        System.out.println("You are now in the display tabular region.");


    }


    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        //Students[] s = new Students[100];
        Scanner input = new Scanner(System.in);

        System.out.println("*************************MAIN MENU****************************");
        System.out.println("Enter any of the following choices: ");
        System.out.println("1. Enter a new data" );
        System.out.println("2. Display individual data" );
        System.out.println("3. Display data in tabular form");
        System.out.println("*************************MAIN MENU****************************");
        char ch = input.next().charAt(0);

        /*File f =new File("index.dat");
        FileOutputStream f1 = new FileOutputStream(f);
        ObjectOutputStream f3 = new ObjectOutputStream(f3);*/

        switch(ch)
        {
            case '1':
                entry();
                break;
            case '2':
                System.out.println("Please enter the roll no. of the student you want to display the rocord of: ");
                int temproll=input.nextInt();
                show_individual(temproll);
                break;
            case '3':
                display_tabular();
                break;
            default:
                System.out.println("Please Enter a valid choice.");
        }
    }   
}

0 个答案:

没有答案