日期:2014-05-20  浏览次数:20975 次

新手求教Object流的小问题
import java.io.*;

public class TestObject
{
public static void main(String[] args) {
try
{
T t = new T();
t.k = 10;

FileOutputStream fos = new FileOutputStream ("d:java\\Object.txt");
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (t);
oos.flush ();
oos.close ();

FileInputStream fis = new FileInputStream ("d:java\\Object.txt");
ObjectInputStream ois = new ObjectInputStream (fis);
T tReader = (T) ois.readObject ();

System.out.println ( tReader.i + " " + tReader.j + " " + tReader.d + " " + tReader.k );
}
catch (FileNotFoundException e1) {
e1.printStackTrace ();
}
catch (IOException e)
{
e.printStackTrace ();
}

catch (ClassNotFoundException e2) {
e2.printStackTrace ();
}



}
}

class T implements Serializable
{
int i = 12;
int j = 34;
double d = 2.34;
int k = 23;
}




为什么执行之后总显示
java.io.FileNotFoundException: d:java\Object.dat (系统找不到指定的路径。)
  at java.io.FileOutputStream.open(Native Method)
  at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
  at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
  at TestObject.main(TestObject.java:11)
新手求教

------解决方案--------------------

FileOutputStream fos = new FileOutputStream ("d:java\\Object.txt");
改为
FileOutputStream fos = new FileOutputStream ("d:\\java\\Object.txt");

FileInputStream fis = new FileInputStream ("d:java\\Object.txt");
改为
FileInputStream fis = new FileInputStream ("d:\\java\\Object.txt");