• Post Reply Bookmark Topic Watch Topic
  • New Topic

formatting get lost after streaming

 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I wrote an Servlet which gets an Path return this File as a Stream.Its workin fine but the formatiing get lost.This is very bad caus the file is an XML File and its very hard to read.

I'll post the Code from the Server and the Client
Server(my Servlet) :
<pre>
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = res.getWriter();
out = new BufferedWriter(pw);
String line;
while ((line = br.readLine()) != null) {
out.write(line);
}
br.close();
out.flush();
out.close();

Client(a Swing Client)
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String line;
while ((line = br.readLine()) != null) {
writer.write(line);
writer.newLine();
}
writer.flush();
writer.close();
isr.close();
br.close();
</pre>
I find out that that at the Client side the readline is only once executed - that means the whole Stream was converted to one big String
Can anybody tell me how to avoid that - should i use another Writer/Reader ?
Thx,
Holger
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is happenning in your servlet. You are using the PrintWriter method write(String) which writes a String on to the stream without a newline. If you use the println(String) method, it will write a new line after the string and your formatting will be preserved.
Server(my Servlet) :

Hope this helps
[This message has been edited by Carl Trusiak (edited January 04, 2001).]
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carl,
first of all thank you - the formatting is preserved now.
But i have to correct your sample Code.
Thres no println() Method in the Class BufferedWriter, so
<pre>
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = res.getWriter();
out = new BufferedWriter(pw);
String line;
while ((line = br.readLine()) != null) {
out.println(line);//The only change from write to println
}
br.close();
out.flush();
out.close();
</pre>
will not work.But you are right, the newline Character get lost.
You can use PrinWriter with its println() Method.But i think this is quit slow.
Another possebility is to use
<pre>
while ((line = br.readLine()) != null) {
out.write(line);
out.newLine();
}
</pre>
so you send the newLine character explicit.
So thx for your posting you was showing me the right direction.

I wish you a happy new Year
Thx,

Holger
[This message has been edited by Holger Prause (edited January 04, 2001).]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually since you created your BufferedWriter Object using a PrintWriter Object, the println method is available to you. Personnally I haven't performed any performance testing on using println verse write then newLine so, I can't advise you on this.
I'm glad you've got your application working
Let us know if you need anything else.
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>actually since you created your BufferedWriter Object using a >PrintWriter Object, the println method is available to you.

>Really ?

ill post if ill need some additional help
thx again,
Holger
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!