Forums Register Login

Funny output

+Pie Number of slices to send: Send
Hi,

I used the following codes and i keep getting the funny outputs below, how do i rectify the problem?? i tried to initialise variable 'ch' to different values but still fails. Any advise???

try {
BufferedReader in = new BufferedReader(new FileReader("file"));
PrintWriter pw = new PrintWriter(new FileWriter("modifiedfile"));//
int ch;
char c;
ch = in.read();
c = (char) ch;
while (ch != -1) {
String str = ""+c;
ch = in.read();
c = (char) ch;
System.out.print(c);
pw.write(ch);
}
in.close();
pw.close();
}catch (IOException ioe) {
System.out.println("There was a problem reading the file");
}
}

Output
======
ame : James
Date : 6th July 2004?


Regards
Shilong
+Pie Number of slices to send: Send
First, you should probably take a browse through the Java Tutorial - Reading and Writing chapter. Lots of good information in there for the beginner.
Second, you should realize that if you do two reads before you write something out, you will not print out the results of that first read.
Third, you should take a look at the documentation for java.io.BufferedReader. BufferedReader makes input more efficient by using an internal buffer to save slow disk access. It also has a convenient method for reading a line of text at a time. Using that method would be faster and easier than reading your data one character at a time.
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 724 times.
Similar Threads
unable to retrieve ms word files from server socket
urgent please help...look at code
Automated HTTP Session
System.out.print is printing 1 word in a single line?
Drop part of a line
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:09:11.