• Post Reply Bookmark Topic Watch Topic
  • New Topic

copying files  RSS feed

 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a issue in copying the files.

I get the input file using

InputStream in = getClass().getResourceAasStream("File name");

in this case What I have is a Input stream object . If i wanted to use file channel it needs a FileInputstream object.

how to convert to filestream object. If not how to copy the files using input stream object.

Thanks in advance
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I dont know of any method to convert it to a FileInputStream object, becuase there is no constructor for FileInputStream that takes InputStream as an argument.

But you could make use of a BufferedInputStream which wraps the InputStream with a BufferedOutputStream to copy a file.

try the following :


This program copies a file passed in as argument 1 to argument 2.

For example : Invoke this as to copy source.txt to dest.txt
----------------

---------------
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton!!

One more suggestion. If I need to copy all the files from a particular director to the destination directory how to handle this?
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can use the listFiles method of the java.io.File class for this. All you have to do is to create a File instance for the path of the directory which you need to access,then use the listFiles method on it. It will return an array of File objects. you can use the previous code which I posted for each of these individual files.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!