• Post Reply Bookmark Topic Watch Topic
  • New Topic

copy method in java.nio.file.Files  RSS feed

 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, anyone please tell me whats wrong with this snippet of code?

- I am trying to copy user entered data into a file. But I do not know how User will indicate end of data...



and please let me know if this approach is not possible at all...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 4, the Files.copy(...) method will copy everything it can read from System.in into the file. But this method will not return until it cannot read any more data from System.in - which means it won't return until System.in is closed.

Line 5 won't be reached before System.in is closed, yet you try to read a byte from System.in there. That will produce an IOException since the input stream is already closed.

It appears you want to read characters from System.in and copy them into a file until a '+' character is read. If that's the case, then you should not use Files.copy. You should instead read just one byte from System.in in the loop, and write that to the file.
 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper you are right
I was playing around and it occurred to me -
if we can use method to send a file to STDOUT,
why not use same method to send STDIN to a file
So I was checking the feasibility of same.

below line of code works however, to indicate data end (i.e. to close STDIN), one has to terminate the program itself...


I was looking for something that lets USER to indicate - that's all data I wanted to input here...

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!