How to rectify void not allowed here and java.io.PrintStream can not be applied here error?

advertisements

There is method which is generating content.html.

contentPage(new PrintStream(socket.getOutputStream(), false));

I want to see the content, I tried

br.write("Content Page: : : "+contentPage());

but got the error

contentPage(java.io.PrintStream) can not be applied..

I tried

br.write("Content Page: : : "+contentPage(ps));

where ps is reference for PrintStream, and got the error

void type not allowed here

I am new to Java, Kindly help me out to print the content.


You could call contentPage(ps) twice:

contentPage(new PrintStream(socket.getOutputStream(), false));
contentPage(System.out);

to output the html to the console,

Or, you can put the html in a buffer first:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos, false);
contentPage(ps);
ps.close();
byte[] data = bos.toByteArray();

OutputStream out = socket.getOutputStream();
out.write(data);

br.write("Content Page: : : " + new String(data));