Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create a streamwriter

Status
Not open for further replies.

Umamageswari

Programmer
Mar 18, 2003
7
IN
Hi,

I want to put a vector into a stream. For that I have created a StreamWriter and I am writing the vector. Below is the code and error message I get.


class StreamWriter
{
void handlestream(String[] str,Vector t)
{
Vector temp = new Vector();

for(int i=0;i<=t.size();i++)
{
temp.addElement(t.elementAt(i));
System.out.println(temp);
}
StreamWriter newstream = new StreamWriter();
newstream.write(temp);
}
}


pakstream\StreamWriter.java:22: cannot resolve symb
symbol : method write (java.lang.String)
location: class pakstream.StreamWriter
newstream.write(&quot;Hello&quot;);
^
1 error



Pls say the coding is correct or not and what is the error. Pls help me in this regard.

thanks.
Uma
 
The Java compiler is complaining because it cannot find a method named write in your StreamWriter class. A quick check of your code shows that, indeed, there is no method named write. Since your StreamWriter class does not extend anything, we cannot expect Java to find the method in a super class.

You can either extend (or create an instance of) one of the JDK's output stream writers to write your Vector object.

---

import java.io.*;
import java.util.Vector;

class StreamWriter {
void handlestream(String[] str,Vector t) throws IOException {
Vector temp = new Vector();

for(int i=0;i<=t.size();i++) {
temp.addElement(t.elementAt(i));
System.out.println(temp);
}

//StreamWriter newstream = new StreamWriter();
//newstream.write(temp);

FileOutputStream fileStream = new FileOutputStream( &quot;Filename&quot; );
ObjectOutputStream newStream = new ObjectOutputStream( fileStream );
newStream.writeObject( temp );
newStream.close();
fileStream.close();

}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top