The Artima Developer Community
Sponsored Link

Java Answers Forum
write-method in my class

1 reply on 1 page. Most recent reply: Nov 25, 2002 5:40 PM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Julia

Posts: 6
Nickname: julia
Registered: Nov, 2002

write-method in my class Posted: Nov 24, 2002 4:08 PM
Reply to this message Reply
Advertisement
Has anyone ideas whats wrong in the
write-method?


/** A TVScheduleWriter is an object that provides its user with an interface for writing
the data contained in TVSchedule objects into a text file. The writer writes schedule 
objects in a file sequentially as its method write is called.

In the text files created by the writer, each "program broadcasting unit" (e.g. a day) 
is represented by a number of lines. The first two lines contain the name and 
broadcasting time of the program schedule, and the following lines contain the titles 
and durations of the individual TV programs, in broadcasting order. There may be an arbitrary 
number of programs in each broadcasting unit; the end of a unit is marked in the 
file by a line consisting of three hyphenation marks. E.g. 
*/
 
 
 
import java.io.*;
 
public class TVScheduleFileWriter extends Object {
 
  public static final String SEPARATOR_LINE = "---";
  private Writer characterStream;
  private BufferedWriter bufferedStream;
 
        public TVScheduleFileWriter(String filepath)
                               throws IOException {
          try {
          Writer characterStream = new FileWriter(filepath);
          BufferedWriter bufferedStream = new BufferedWriter(characterStream);
          } catch (Exception e){
          throw new IOException("wrong");
          }
 
        }
 
 
/**
Writes (into the file) the data for one "broadcasting unit" (e.g. a day). The given data is appended after 
the data written when this method was last called.
Parameters:
schedule - the program schedule data to be stored in 
the file
Throws:
IOException - if problems arose when handling the file

*/
        public void write(TVSchedule schedule) throws IOException {
          try {
            bufferedStream.write(schedule);
            bufferedStream.newLine();
          } catch (Exception e) {
            throw new IOException("wrong");
          }
 
        }
 
 
        public void close() throws IOException {
          try {
          bufferedStream.close();
          characterStream.close();
          } catch (Exception e) {
            throw new IOException("wrong")
          }
        }
 
}
 

_________________________________________________

My problem is how to write the write-method.
It is not right..

TVScheduleFileWriter.java:22: cannot resolve symbol
symbol : method write (TVSchedule)
location: class java.io.BufferedWriter
bufferedStream.write(schedule);
^


Javadoc of TVSchedule-class is
http://www.hut.fi/~t106213/english/exercises/unit_5/exercise_3/documents/


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: write-method in my class Posted: Nov 25, 2002 5:40 PM
Reply to this message Reply
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

Flat View: This topic has 1 reply on 1 page
Topic: how to store addressbook values using Vectors Previous Topic   Next Topic Topic: resultset from a store proc

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use