wood burning stoves 2.0*
The moose likes Java in General and the fly likes Unexpected characters using PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unexpected characters using PrintWriter" Watch "Unexpected characters using PrintWriter" New topic
Author

Unexpected characters using PrintWriter

matt woodford
Greenhorn

Joined: Sep 17, 2009
Posts: 2
Hi, I am using Velocity to generate a script file, the script file producted however contains an extraneous last line as follows:

'java.io.PrintWriter@5f805f80'

The code is:

private void createDSJython(){
try{
//build Jython
VelocityEngine ve = new VelocityEngine();
ve.init();
VelocityContext context = new VelocityContext();
context.put("jndiName", dsAttrs);
Template t = ve.getTemplate( "testDs.vm" );
/* now render the template into a Writer */

BufferedWriter output = null;
File file = new File("D://createDs.py");
FileWriter fl = new FileWriter(file);
output = new BufferedWriter(fl);
PrintWriter print = new PrintWriter(output);
t.merge(context, print);
print.write(print.toString());

if (print != null) {
print.flush();
print.close();
}
}catch(Exception e){
e.printStackTrace();
}
}

and the Velocity templat, 'testDs.vm' is:

#foreach( $jndiName in $jndiName )
AdminTask.createDatasource(newjdbc, '[-name "ValidSystems DataSource" -jndiName $jndiName.getJndiName() -dataStoreHelperClassName com.ibm.websphere.rsadapter.DB2UniversalDataStoreHelper -componentManagedAuthenticationAlias ' + uname + ' -xaRecoveryAuthAlias ' + uname + ' -configureResourceProperties [[databaseName java.lang.String CJSE] [driverType java.lang.Integer 4] [serverName java.lang.String ' + serverName + '] [portNumber java.lang.Integer ' + portNumber + ']]]')
#end

Any help, much appreciated!

Thanks,

Matt
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Please Use Code Tags.
print.write(print.toString());

PrintWriter inherits toString() directly from Object, and that's simply what Object's toString() returns - the fully qualified class name (java.io.PrintStream) followed by a @ and the hash code in HEX. As far as I know there is only one Writer that returns its contents with toString(), and that's StringWriter.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Unexpected characters using PrintWriter
 
Similar Threads
Velocity Template is not encoding charecter,instead throwing an error message.
Velocity Templates with Servlets
Configuring Velocity - ResourceNotFoundException
Is Velocity thread safe?
Velocity Templates with Servlets