• Post Reply Bookmark Topic Watch Topic
  • New Topic

Filereader  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It just one time print:

This
is
an
example


But it must print twice.Why does n't it print twice?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your reader reached the end of the file. You may not read again using it.
Initialize a new reader.

You don't need to flush a writer (line 20) prior to closing it (line 21).
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean about should print twice?
Why are you using the read method? Don't use a file reader on its own. Use buffered reader, or better still learn about the newer file tools. The read method only reads one int at a time. You probably have nothing to print in the array.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do you mean about should print twice?

Look at line 26. OP reads whole file here to char array and then attempts to read again using a loop with the same reader.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 27 you are reading 50 characters from the file. As the whole content is smaller this reads the whole content. You then output the content read.

Beginning from line 32 you read from the same file again, character for character. But you are not reading from the same file again only, you are reusing the same FileReader that you didn't construct again or reset.

A Reader has a state, it remembers the position from which to read the next character (otherwise reading character by character couldn't work). So when you read everything in line 27 you positioned the pointer at the end of the file. When trying to read more from the file there simple isn't anything left.

So you need to reset the reader or reconstruct it.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:So you need to reset the reader or reconstruct it.

Exception in thread "main" java.io.IOException: reset() not supported
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Hauke Ingmar Schmidt wrote:So you need to reset the reader or reconstruct it.

Exception in thread "main" java.io.IOException: reset() not supported


...if the reset operation is supported by the specific reader. Sorry, I should have checked (or even remembered) that. So few reader support that operation, it shouldn't have made it into the general API.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I missed line 26, or thought read() would only find a null character. Sorry for my mistake.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!