Jay Kandy
Posts: 77
Nickname: jay
Registered: Mar, 2002
|
|
Re: write-method in my class
|
Posted: Nov 25, 2002 5:40 PM
|
|
Whats wrong is that the write() method of java.io.BufferedWriter does not know about the class TVSchedule. You have two choices:
1. If you want to store an "object" of TVSchedule, you can do it this way:
TVSchedule aTVSchedule = ...;
FileOutputStream fos = new FileOutputStream("tv.ser");
ObjectOutputStream ostream = new ObjectOutputStream(fos);
ostream.writeObject(aTVSchedule);
Remember this saves the state of the object so you can read that later. It'd be cool to have a toString() method for TVSchedule. With that, your original write() method should work like a charm.
2. But if you wanted to print out the TVSchedules, you would do it this way:
public void write(TVSchedule schedule) throws IOException
{
try
{
StringBuffer buffer = new StringBuffer();
buffer.append("Channel: ");
buffer.append(schedule.getChannel());
buffer.append("Programs:");
buffer.append("\n");
TVProgramInfo info[] = channel.getPrograms();
for(int i=0; i<info.length;)
{
buffer.append(info[i].getName());
buffer.append("\n");
}
//...and then, pass this String to
// the write method...
bufferedStream.write(buffer.toString(), 0, buffer.length());
bufferedStream.newLine();
// ...how about if we flushed?
bufferedStream.flush();
}
catch (Exception e)
{
throw new IOException("wrong");
}
}
Even if it doesnt compile, hope you get the idea. Also, why would you name
|
|