• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

copy method in java.nio.file.Files

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic