java序列化:-程序在writeObject()方法时终止

时间:2018-08-28 16:56:06

标签: java serialization deserialization

我开始学习Java序列化。我写了一个简单的程序,从客户端程序发送选择和对象。选择是成功通过传递,但是程序在到达writeObject()方法后立即终止。

请帮助!。 这是客户端:-

import java.io.*;
import java.net.*;
import java.util.Scanner;


public class client implements Serializable
{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@SuppressWarnings("resource")
public static void main(String args[]) throws IOException, Exception
{
    @SuppressWarnings("resource")
    Socket s=new Socket("127.0.0.1",9339);
    System.out.println("Connection successful");

    //to get input from keyboard
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    Scanner sc=new Scanner(System.in);


    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());

    int ch1,ch2;
    int choice;
    try
    {
        do
        {
            System.out.println("Enter your Choice :" );
            System.out.println("1.Add Student"+System.lineSeparator()+"2.Show Student");
            System.out.println("3.search Student"+System.lineSeparator()+"4.update Student");
            System.out.println("5.delete Student"+System.lineSeparator()+"6.Show fail Student");
            choice=sc.nextInt();
            out.writeInt(choice);
            switch(choice)
            {
            case 1:
            {
                do
                {
                    student stud=new student();
                    System.out.println("Enter Roll no. of Student : ");
                    stud.roll=Integer.parseInt(br.readLine());
                    System.out.println("Enter name of Student : ");
                    stud.name=br.readLine();
                    System.out.println("Enter Percentage : " );
                    stud.percent=sc.nextFloat();
                    System.out.println("Enter Attendance : ");
                    stud.attendance=sc.nextInt();
                    System.out.println("Enter Password of Student : ");
                    stud.pass=br.readLine();
                    out.writeObject(stud);
                    System.out.println("in Client Object is pass");

                    System.out.println("Do you want to add more (1.Yes/0.No)");
                    ch2=sc.nextInt();
                }while(ch2==1);
            }
            break;
            case 2:
            {

            }
            break;
            case 3:
            {

            }
            break;
            case 4:
            {

            }
            break;
            case 5:
            {

            }
            break;
            case 6:
            {

            }
            break;
            default :
                System.out.println("Error : Invalid Choice !!");
            }
            System.out.println("Do you want to Continue (1.Yes/0.No)");
            ch1=sc.nextInt();
        }while(ch1==1);
    }
    catch(Exception e)
    {

    }

}
}

这是服务器端:-

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;


public class server  implements Serializable
{
/**
 * 
 */
   private static final long serialVersionUID = 1L;
   Enumeration <student> stud;
   Scanner sc =new Scanner(System.in);
   BufferedReader br=new BufferedReader(new 
InputStreamReader(System.in));
@SuppressWarnings("unused")
public static Vector <student> arr=new <student> Vector<student>();


public void addStudent(student obj) throws NumberFormatException, IOException
{
    System.out.println("Enter Roll no. of Student : ");
    obj.roll=Integer.parseInt(br.readLine());
    System.out.println("Enter name of Student : ");
    obj.name=br.readLine();
    System.out.println("Enter Percentage : " );
    obj.percent=sc.nextFloat();
    System.out.println("Enter Attendance : ");
    obj.attendance=sc.nextInt();
    System.out.println("Enter Password of Student : ");
    obj.pass=br.readLine();
}

public void showAttend(student obj)
{
    System.out.println("Attendance : "+obj.attendance);             
}
public void showPercent(student obj)
{
    System.out.println("Attendance : "+obj.percent);        
}


public void showStudent(student obj)
 {
     System.out.println("Student rollno : "+obj.roll);
     System.out.println("Student name : "+obj.name);
     System.out.println("Student Percentage : "+obj.percent);
     System.out.println("Student Attendance : "+obj.attendance);
 }
public void Display() //using itreator
{
    Iterator <student> itr= arr.iterator(); 
    while(itr.hasNext())
    {
        System.out.println(itr.next().toString());
    }
}
public void Show_Enumeration()
{
    stud=arr.elements();
    while(stud.hasMoreElements())
    {
        System.out.println(stud.nextElement().toString());
    }

}
public void fail(student obj)
{

    if(obj.percent<35)
    {
         this.showStudent(obj);
    }
}
public void update() throws NumberFormatException, IOException
{
    int temp;
    System.out.println("Enter rollno of Student to update ");
    temp=sc.nextInt();
    int index=search(temp);
    if(index!=-1)
    {
        arr.remove(index);
        System.out.println("Ente new updated Data : ");
        student obj=new student();
        arr.add(obj);
    }
}
public void delete()
{
    int temp;
    System.out.println("Enter rollno of Student to delete ");
    temp=sc.nextInt();
    int index=search(temp);
    if(index!=-1)
    {
        arr.remove(index);
        System.out.println("Student deleted succesfully: ");
    }
}
public <T> int search(T temp)
{
    int flag=0,index=-1;
    Integer r;
    String n;
    if(temp instanceof Integer)
    {
        r=(Integer)temp;
        for(student obj: arr)
        {
            if(obj.roll==r)
            {
                flag=1;
                index=arr.indexOf(obj);
                break;
            }

        }
        if(flag==1)
        {
            System.out.println("Record found Succefull ! ");

            return index;
        }
        else
        {
            System.out.println("Record not found !");
            return -1;
        }
    }

    if(temp instanceof String)
    {
        n=(String) temp;
        for(student obj: arr)
        {
            if(obj.name.equalsIgnoreCase(n))
            {
                flag=1;
                index=arr.indexOf(obj);
                break;
            }

        }
        if(flag==1)
        {
            System.out.println("Record found Succefull ! ");

            return index;
        }
        else
        {
            System.out.println("Record not found !");
            return -1;
        }
    }
    return 0;
}


public static void main(String args[]) throws IOException
{
    ServerSocket ss=new ServerSocket(9339);
    Socket s=ss.accept();

    // to read from keyboard
    Scanner sc=new Scanner(System.in);
    new BufferedReader(new InputStreamReader (System.in));


    ObjectInputStream in=new ObjectInputStream(s.getInputStream());

    int choice;
    server t=new server();
    try
    {
                    choice=in.readInt();
                    System.out.println("Choice in techer is"+choice);
                    switch(choice)
                    {
                    case 1:
                        {
                            student stud;
                            stud=(student)in.readObject();
                            arr.add(stud);
                        }
                        break;
                    case 2:
                    {
                        t.Display();// using iterator
                        //t.Show_Enumeration(); // using enumeration
                    }
                    /*case 2:
                    {
                        for(Studentinfo obj : teacharinfo.arr)
                        {
                            t.showStudent(obj);
                        }
                    }*/
                    break;
                    case 3:
                    {
                        int index,cc;
                        System.out.println("Enter your choice : ");
                        System.out.println("1.Search by roll no."+System.lineSeparator()+"2.Search by Name");
                        cc=sc.nextInt();
                        if(cc==1)
                        {
                            System.out.println("Enter roll no. : ");
                            int temp=sc.nextInt();
                            index=t.search(temp);
                        }
                        else
                        {
                            System.out.println("Enter name : ");
                            String temp=sc.next();
                            index=t.search(temp);
                        }

                        student obj=new student();
                        obj=arr.get(index);
                        t.showStudent(obj);
                    }
                    break;
                    case 4:
                    {
                        t.update();
                    }
                    break;
                    case 5:
                    {
                        t.delete();
                    }
                    break;
                    case 6:
                    {
                        System.out.println("Fail Student : ");
                        for(student obj:arr)
                        {
                            t.fail(obj);
                        }
                    }
                    break;
                    }

    }
    catch(Exception e)
    {

    }
    ss.close();
    s.close();
    sc.close();

}
}

学生班级定义:-

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Scanner;

public class student implements Serializable
{
/**
 * 
 */
    private static final long serialVersionUID = 1L;
    public int roll;
    public String name;
    public int attendance;
    public float percent;
    public String pass;
    BufferedReader br=new BufferedReader( new 
InputStreamReader(System.in));
    Scanner sc=new Scanner(System.in);



public String toString()
{
    return "Student rollno : "+this.roll+System.lineSeparator()+"Student name : "+this.name+System.lineSeparator()+"Student Percentage : "+this.percent+System.lineSeparator()+"Student Attendance : "+this.attendance;
}
}

0 个答案:

没有答案