• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Funny output

Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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;
}catch (IOException ioe) {
System.out.println("There was a problem reading the file");

ame : James
Date : 6th July 2004?

Posts: 9612
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
Two software engineers solve most of the world's problems in one K&R sized book
    Bookmark Topic Watch Topic
  • New Topic