对象流中的小问题(100分)

  • 主题发起人 主题发起人 puremoonstone
  • 开始时间 开始时间
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类呢?

 
在ObjectToDisk.java文件中第一行中加入package ObjToDisk
然后在文件ObjectFromDisk.java中加入import ObjToDisk.*
 
谢谢slong,我明天去试。
同样的下面的程序也有这个问题:
package com.prefect.ecommerce;
import java.util.*;
public class Item implements Comparable
{
private String id;
private String name;
privatedo
uble retail;
private int quantity;
privatedo
uble price;

Item(String idIn,String nameIn,String retailIn,String quanIn)
{
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn);
if (quantity>400)
{
price=retail*0.5D;
}
else
if (quantity>200)
{
price=retail*0.6D;
}
else
{
price=retail*0.7D;
}
price=Math.floor(price*100+0.50)/100;
}
public int compareTo(Object obj)
{
Item temp=(Item)obj;
if (this.price<temp.price)
{
return 1;
}
else
if (this.price>temp.price)
{
return -1;
}
else
{
return 0;
}
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
publicdo
uble getRetail()
{
return retail;
}
public int getQuantity()
{
return quantity;
}
publicdo
uble getPrice()
{
return price;
}
}


package com.prefect.ecommerce;
import java.util.*;
public class Storefront
{
private LinkedList catalog=new LinkedList();
public void addItem(String id,String name,String price,String quant)
{
Item it=new Item(id,name,price,quant);
catalog.add(it);
}
public Item getItem(int i)
{
return (Item)catalog.get(i);
}
public int getSize()
{
return catalog.size();
}
public void sort()
{
Collections.sort(catalog);
}
}


import com.prefect.ecommerce.*;
public class GiftShop
{
public static void main(String[] arguments)
{
Storefront store =new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("D01","T SHIRT","16.99","90");
store.sort();
for (int i=0;
i<store.getSize();
i++)
{
Item show=(Item)store.getItem(i);
System.out.println("/nItem ID:"+show.getId()+"/nName:"+show.getName()+"/nRetail Price:$"+show.getRetail()+"/nPrice:$"+show.getPrice()+"/nQuantity"+show.getQuantity());
}
}
}
现在Storefront.java,GiftShop.java都不能识别Item.
应该如何组织这3个文件才能让它们编译运行呢(这3个文件如何存放?
系统的CLASSPATH路径该如何设置?)?

此外,还有一个类似的问题:
import java.awt.*;
import java.util.*;
public class Watch extends javax.swing.JApplet
{
private Color butterscotch=new Color(255,204,102);
private String lastTime="";
public void init()
{
setBackground(Color.black);
}
public void paint(Graphics screen)
{
Graphics2D screen2D=(Graphics2D)screen;
Font type=new Font("Monospaced",Font.BOLD,20);
screen2D.setFont(type);
GregorianCalendar day=new GregorianCalendar();
String time=day.getTime().toString();
screen2D.drawString(lastTime,5,25);
screen2D.setColor(butterscotch);
screen2D.drawString(time,5,25);
try
{
Thread.sleep(1000);
} catch(InterruptedException e)
{
System.out.println("Error---"+e.toString());
}
lastTime=time;
repaint();
}
}

<html>
<head>
<title>Watch Applet</title>
</head>
<body>
<applet code="Watch.class" height="50" width="345">
This program requires a Java_enabled browser.
</applet>
</body>
</html>
当Watch.class,Watch.html和appletviewer都在同一目录(c:/jdk1.3.1_01/bin)时,
运行没有问题,如果将Watch.class,Watch.html放到c:/test,系统提示找不到文件,
请问该如何设置才能使
appletviewer c:/test/Watch.html 可以正常运行 ?
 
to slong:
在ObjectToDisk.java开头加入“package ObjectToDisk”之后,编译后运行提示错误:
Exception in Thread main java.lang.NoClassDefError:ObjectToDisk(wrong name:ObjectToDisk/ObjectToDisk).
如果只在ObjectFromDisk.java中加入import ObjectToDisk.*,运行ObjectFromDisk则出现错误:java.lang.ClassCastException:ObjectToDisk$Message.
 
我试了一下,以下可以:
class Message2 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;
}
}

ObjectToDisk2.java如下:
import Message2.*;
import java.io.*;
import java.util.*;
public class ObjectToDisk2
{
public static void main(String[] arguments)
{
Message2 mess=new Message2();
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//Message2.obj");
ObjectOutputStream oo=new ObjectOutputStream(fo);
oo.writeObject(mess);
oo.close();
System.out.println("采用新方法成功创建对象!");
} catch(IOException e)
{
System.out.println("Error---"+e.toString());
}
}

}

ObjectFromDisk2.java如下:
import Message2.*;
import java.io.*;
import java.util.*;
public class ObjectFromDisk2
{
public static void main(String[] arguments)
{
try
{
FileInputStream fi=new FileInputStream("c://test//Message2.obj");
ObjectInputStream oi=new ObjectInputStream(fi);
Message2 mess=(Message2)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());
}
}
}
 
接受答案了.
 
后退
顶部