import java.io.*;
import cs1.Keyboard;
public class TestSerializable 
{
public static void main(String arg[])
{
TestSerializable test = new TestSerializable();
test.writeBooks(2,"D:\\hello.dat");
test.readBooks("D:\\hello.dat");
}
private void writeBooks(int number,String fileName)
{
try
{
FileOutputStream out = new FileOutputStream(fileName);
System.out.println("准备输入" + number + "本书的数据信息");
for(int i= 0; i<number ; i++)
{
String name;
double price;
System.out.println("现在输入第" + (i+1) + "书本的信息" );
System.out.print("输入书名:");
name = Keyboard.readString();
System.out.println("输入书价:");
price = Keyboard.readDouble();
Book book = new Book(name,price);
book.writeToFile(out);
}
out.close();

}catch(IOException ioe)
{
System.out.println("writeBook()"+ioe.getMessage());
}
}
private void readBooks(String fileName)
{
try
{
FileInputStream in = new FileInputStream(fileName);
try
{
System.out.println("书籍列表:");
System.out.println("ID" + "\t" + "书名" + "\t" + "价格");
while(true)
{
Book book = new Book();
book.readFromFile(in);
System.out.println(book);
}
}
catch(EOFException eof)
{
System.out.println("0");
}finally
{
in.close();
}
}catch(FileNotFoundException e)
{
System.out.println("1"+e.getMessage());
}
catch(ClassNotFoundException e)
{
System.out.println("2"+e.getMessage());
}
catch(IOException iox)
{
System.out.println("3"+iox.getMessage());
}
} class Book implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
transient int id;
String bookName;
double price;
public  int nextID = 1;

public Book()
{
this("",0.0);
this.id = nextID++;
}
public Book(String bookName,double price)
{
this.bookName = bookName;
this.price = price;
}
public void writeToFile(FileOutputStream out) throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(this);
oos.flush();
}
public void readFromFile(FileInputStream in)throws IOException,ClassNotFoundException
{
ObjectInputStream ois = new ObjectInputStream(in);
Book temp = (Book)ois.readObject();
this.bookName = new String(temp.bookName);
this.price = temp.price;

}
public String toString()
{
return id + "\t" + bookName + "\t" + price;
}
}
}
这段代码用eclipse运行一开始没有抛出异常,后来运行就不行了状况如下:
writeBook()TestSerializable
书籍列表:
ID 书名 价格
3writing aborted; java.io.NotSerializableException: TestSerializable找了好久也不知道哪里错了,求高手帮忙看看~
Keyboard 类是从网上找的,应该没问题

解决方案 »

  1.   

    应该是Keyboard类有问题,你用System.in输入流试试看
      

  2.   


    //你改为下面的写法就OK了
    //有时候内部类也会带来极大的麻烦,就好像这一例
    import java.io.*;import cs1.Keyboard;
    public class TestSerializable {
        public static void main(String arg[]) {
    TestSerializable test = new TestSerializable();
    test.writeBooks(2, "D:\\hello.dat");
    test.readBooks("D:\\hello.dat");
        }    private void writeBooks(int number, String fileName) {
    try {
        FileOutputStream out = new FileOutputStream(fileName);
        System.out.println("准备输入" + number + "本书的数据信息");
        for (int i = 0; i < number; i++) {
    String name;
    double price;
    System.out.println("现在输入第" + (i + 1) + "书本的信息");
    System.out.print("输入书名:");
    name = Keyboard.readString();
    name = "name" + number;
    System.out.println("输入书价:");
    price = Keyboard.readDouble();
    price = 0.00 + number;
    Book book = new Book(name, price);
    book.writeToFile(out);
        }
        out.close(); } catch (IOException ioe) {
        System.out.println("writeBook()" + ioe.getMessage());
    }
        }    private void readBooks(String fileName) {
    try {
        FileInputStream in = new FileInputStream(fileName);
        try {
    System.out.println("书籍列表:");
    System.out.println("ID" + "\t" + "书名" + "\t" + "价格");
    while (true) {
        Book book = new Book();
        book.readFromFile(in);
        System.out.println(book);
    }
        } catch (EOFException eof) {
    System.out.println("0");
        } finally {
    in.close();
        }
    } catch (FileNotFoundException e) {
        System.out.println("1" + e.getMessage());
    } catch (ClassNotFoundException e) {
        System.out.println("2" + e.getMessage());
    } catch (IOException iox) {
        System.out.println("3" + iox.getMessage());
    }
        }
    }class Book implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        transient int id;
        String bookName;
        double price;
        public int nextID = 1;    public Book() {
    this("", 0.0);
    this.id = nextID++;
        }    public Book(String bookName, double price) {
    this.bookName = bookName;
    this.price = price;
        }    public void writeToFile(FileOutputStream out) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(this);
    oos.flush();
        }    public void readFromFile(FileInputStream in) throws IOException,
        ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(in);
    Book temp = (Book) ois.readObject();
    this.bookName = new String(temp.bookName);
    this.price = temp.price;    }    public String toString() {
    return id + "\t" + bookName + "\t" + price;
        }
    }