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 类是从网上找的,应该没问题
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 类是从网上找的,应该没问题
解决方案 »
- java学生管理从数据库读数据问题求解
- 求助有用过JDateChooser的吗
- java.sql.Date 和java.util.Date 怎么互相转换?
- java 斯坦福大学的一道java编程题(self-avoiding random walk)
- 关于JTree的事件处理问题
- 初学者的一个小问题,请各位高手朋友不要笑话,本人刚开始学,不过10天.
- 关于JAVA连接SQLServer2000的问题,为什么就是提示驱动程序找不到呢?
- 总也喜欢不上java
- VJ++ & Java
- JBuilder5需要多大的硬盘和内存?
- 求解byte转换int,int转换byte
- 如何更改才能得到所要的输出结果?
//你改为下面的写法就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;
}
}