sky5412
2020-06-19 08:53
采纳率: 0%
浏览 226
已采纳

IO流反序列化读取文件出现ClassNotFoundException错误

用IO流反序列化读取老师给的已写好的文件时会出现ClassNotFoundException错误,但是如果自己写好文件,再反序列化读取却不会报错
代码如下:

package Main;



import java.io.File;



import java.io.FileOutputStream;



import java.io.OutputStream;



import java.io.ObjectOutputStream;



import java.io.FileInputStream;



import java.io.ObjectInputStream;



import java.io.Serializable;



import java.util.Scanner;

class Book implements Serializable {//



    private String bookName;// 书名



    private String ISBN;// 书的ISBN



    private double price;// 单价



    private int number;// 数量



    public String getBookName() {//



        return bookName;



    }



    public void setBookName(String bookName) {



        this.bookName = bookName;



    }



    public String getISBN() {



        return ISBN;



    }



    public void setISBN(String iSBN) {



        ISBN = iSBN;



    }



    public double getPrice() {



        return price;



    }



    public void setPrice(double price) {



        this.price = price;



    }



    public int getNumber() {



        return number;



    }



    public void setNumber(int number) {



        this.number = number;



    }



    public Book(String bookName, String iSBN, double price, int number) {



        super();



        this.bookName = bookName;



        ISBN = iSBN;



        this.price = price;



        this.number = number;



    }



    @Override



    public String toString() {//



        return "bookName:" + bookName + ", ISBN:" + ISBN + ", price:"



                + price + ", number:" + number;



    }



}



public class Main {



    public static void main(String args[]) throws Exception {

        Scanner cin = new Scanner(System.in);

        int count = 0;
        //写入文件部分,自己写入再读取不会报错,直接读取老师给的文件会报错
//需要先输入图书信息

//        int n = cin.nextInt();   //书本数
//
//        Book book1[] = new Book[n];
//
////写入图书数组
//
//        for (int i = 0; i < n; i++) {
//
//            book1[i] = new Book(cin.next(), cin.next(),cin.nextDouble(), cin.nextInt());
//
//        }
//
//        write(book1);//

//读取图书数组

        Book[] book = (Book[])read();

        for(Book bk : book) {

            System.out.println(bk);

            count++;

        }

//        System.out.println("请输入学号:");
//
//        String id = cin.next();
//
//        System.out.println("请输入姓名:");
//
//        String name = cin.next();
//
//        int price = 0;
//
//        File f = new File("D:"+File.separator +id+".txt");
//
//        FileOutputStream fos = new  FileOutputStream(f);
//
//        fos.write((id+" ").getBytes());
//
//        fos.write((name+"\r\n").getBytes());
//
//        fos.write(("有"+count+"本书。"+"清单如下:\r\n").getBytes());
//
//        for(Book bk : book) {
//
//            fos.write((bk.toString()+"\r\n").getBytes());
//
//            price += bk.getPrice()*bk.getNumber();
//
//        }
//
//        fos.write(("共需要"+price+"元\r\n").getBytes());
//
//        fos.close();

    }



    public static void write(Object obj[]) throws Exception {



        File f = new File("D:" + File.separator + "file.dat"); //



        ObjectOutputStream oos = null; //



        OutputStream out = new FileOutputStream(f); //



        oos = new ObjectOutputStream(out);//



        oos.writeObject(obj); //



        oos.close(); //



    }


    public static Object[] read() throws Exception {

        Object[] bk = null;

        File f = new File("D:" + File.separator + "file.dat");

        FileInputStream fis = new FileInputStream(f);

        ObjectInputStream ois = new ObjectInputStream(fis);

        bk = (Object[])ois.readObject();

        ois.close();

        return bk;


    }



}

这是自己输入的内容:
3
离散数学题解 9787302325086 23 1
Java开发实战经典 9787302483663 118 50
深度学习入门之PyTorch 9787121326202 79 2

请问这是为什么呢?

1条回答 默认 最新

相关推荐 更多相似问题