Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Copy an Image File from One Directory to Another  RSS feed

 
Tumaini Kilimba
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sirs et Madames,
I am trying to copy and Image file from "C:\Users\Sir Tumaini Kilimba\Desktop\banner-graphic.png" to "C:\Users\Sir Tumaini Kilimba\Desktop\Generic Application\src\main\webapp\images", all via a java application. Can anyone suggest a way of doing this?

Thanks in advance
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard Java way is to open the source file with a FileInputStream, the destination file with a FileOutputStream, read in the data, and write out the data. You have to be careful about buffers and such though. Another way is to get the Apache Commons IO library (free) and look at the FileUtils class. There are a lot of methods there for manipulating files on a file system.
 
Tumaini Kilimba
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote: You have to be careful about buffers and such though.


Thanks Greg... Careful in what sense though?
 
Stephan van Hulst
Saloon Keeper
Posts: 7693
139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also open a FileChannel on the source and destination Files, and then call transferTo() on the source channel, or transferFrom() on the destination channel. This should be quite efficient.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!