P
puremoonstone
Unregistered / Unconfirmed
GUEST, unregistred user!
有这样的一个程序:
import java.io.*;
import java.util.*;
public class ObjectFromDisk
{
public static void main(String[] arguments)
{
try
{
FileInputStream fi=new FileInputStream("c:/test/Message.obj");
ObjectInputStream oi=new ObjectInputStream(fi);
Message mess=(Message)oi.readObject();
System.out.println("Message:/n");
System.out.println("From:"+mess.from);
System.out.println("To:"+mess.to);
System.out.println("Date:"+mess.when+"/n");
for (int i=0;
i<mess.lineCount;
i++)
{
System.out.println(mess.text);
}
oi.close();
} catch(Exception e)
{
System.out.println("Error---"+e.toString());
}
}
}
本程序中读取的Message.obj是由以下程序生成的:
import java.io.*;
import java.util.*;
public class ObjectToDisk
{
public static void main(String[] arguments)
{
Message mess=new Message();
String author="Sam Wainwright,London";
String recipient="George Bailey,Bedford Falls";
String[] letter={"Mr.Gower cabled you need cash.Stop.","My office instructed to advance you up to twenty-five","thousanddo
llars.Stop.Hee-haw and Merry Christmas."};
Date now=new Date();
mess.writeMessage(author,recipient,now,letter);
try
{
FileOutputStream fo=new FileOutputStream("c:/test/Message.obj");
ObjectOutputStream oo=new ObjectOutputStream(fo);
oo.writeObject(mess);
oo.close();
System.out.println("成功创建对象!");
} catch(IOException e)
{
System.out.println("Error---"+e.toString());
}
}
static class Message implements Serializable
{
int lineCount;
String from,to;
Date when;
String[] text;
void writeMessage(String inFrom,String inTo,Date inWhen,String[] inText)
{
text=new String[inText.length];
for (int i=0;
i<inText.length;
i++)
{
text=inText;
}
lineCount=inText.length;
to=inTo;
from=inFrom;
when=inWhen;
}
}
}
当编译ObjectFromDisk时提示can not resolve symbol Message,但Message是在
ObjectToDisk中定义了的
(ObjectFromDisk.java,ObjectToDisk.java,ObjectToDisk.class,Message.obj,
ObjectToDisk$Message.class都在同一个目录c:/test下,classpath中包括c:/test)
该怎样让ObjectFromDisk识别Message类呢?
import java.io.*;
import java.util.*;
public class ObjectFromDisk
{
public static void main(String[] arguments)
{
try
{
FileInputStream fi=new FileInputStream("c:/test/Message.obj");
ObjectInputStream oi=new ObjectInputStream(fi);
Message mess=(Message)oi.readObject();
System.out.println("Message:/n");
System.out.println("From:"+mess.from);
System.out.println("To:"+mess.to);
System.out.println("Date:"+mess.when+"/n");
for (int i=0;
i<mess.lineCount;
i++)
{
System.out.println(mess.text);
}
oi.close();
} catch(Exception e)
{
System.out.println("Error---"+e.toString());
}
}
}
本程序中读取的Message.obj是由以下程序生成的:
import java.io.*;
import java.util.*;
public class ObjectToDisk
{
public static void main(String[] arguments)
{
Message mess=new Message();
String author="Sam Wainwright,London";
String recipient="George Bailey,Bedford Falls";
String[] letter={"Mr.Gower cabled you need cash.Stop.","My office instructed to advance you up to twenty-five","thousanddo
llars.Stop.Hee-haw and Merry Christmas."};
Date now=new Date();
mess.writeMessage(author,recipient,now,letter);
try
{
FileOutputStream fo=new FileOutputStream("c:/test/Message.obj");
ObjectOutputStream oo=new ObjectOutputStream(fo);
oo.writeObject(mess);
oo.close();
System.out.println("成功创建对象!");
} catch(IOException e)
{
System.out.println("Error---"+e.toString());
}
}
static class Message implements Serializable
{
int lineCount;
String from,to;
Date when;
String[] text;
void writeMessage(String inFrom,String inTo,Date inWhen,String[] inText)
{
text=new String[inText.length];
for (int i=0;
i<inText.length;
i++)
{
text=inText;
}
lineCount=inText.length;
to=inTo;
from=inFrom;
when=inWhen;
}
}
}
当编译ObjectFromDisk时提示can not resolve symbol Message,但Message是在
ObjectToDisk中定义了的
(ObjectFromDisk.java,ObjectToDisk.java,ObjectToDisk.class,Message.obj,
ObjectToDisk$Message.class都在同一个目录c:/test下,classpath中包括c:/test)
该怎样让ObjectFromDisk识别Message类呢?