• Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying a file content to another file.  RSS feed

 
Manu Dhanda
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends

I did written this code which is working perfectly fine as it is now.(See code).





But as soon as I change it like when am declaring variable int c and if i do write it like

int c = in.read();

instead of writing "in.read()" in the while loop.

Here I understand that it 'll read only the first character from input file but it keeps on copying the same character in the file "output.txt" and never stop and fill the harddisk space.

Can anyone explain me this face??

 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. You say you changed the above code to look like this:

and you were surprised that the loop never terminated?
If you want to write only one character, why have a loop? If you want the loop to terminate, you have to change the value of c somewhere.
 
Manu Dhanda
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here am confused that while in.read() is in the while loop it always read the next character. But we are not incrementing or giving it any particualr instructions to read the next character. Then why don't it just read the same character here???

 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what read() does:

public abstract int read() throws IOException
Reads the next byte of data from the input stream.

java.io.InputStream
We don't have to tell the stream to move to the next byte. Each call to read() does that automatically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!