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

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条回答 默认 最新

  • dabocaiqq 2020-08-24 16:33
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 11月16日

悬赏问题

  • ¥15 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架