Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to learn something, try it yourself.<br> I can show you the possibilities. Try these ways useful to insert in your code.</p> <p>Question : I am trying now to import a :</p> <ul> <li>1) new image into a certain directory, for example into C:\output</li> </ul> <p><strong>Answer:</strong></p> <p>How to copy file in Java from one directory to another is common requirement, given that there is no direct method in File API for copying files from one location to another.</p> <p>One way of copying file is reading from FileInputStream and writing same data to FileOutputStream to another directory.</p> <p>Thankfully you don't need to reinvent wheel here, there are some open source library available which allows us to copy file in Java easily from one directory to another. One of such library is <strong>Apache commons IO</strong> which contains a class called <strong>FileUtils</strong>, which provides utility method for file related operation.</p> <p><a href="https://stackoverflow.com/questions/5388146/copy-and-rename-file-on-different-location">Copy and rename file on different location</a></p> <p><a href="http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyFile%28java.io.File,%20java.io.File%29" rel="nofollow noreferrer">FileUtils.copyFile</a></p> <p><strong>FileUtils.copyFile(sourceFile, targetFile)</strong> can be used</p> <pre><code> String source = "C:/output/myImage.jpg"; //directory where file will be copied String target ="C:/importImages/"; //name of source file File sourceFile = new File(source); String Filename = sourceFile.getName(); File targetFile = new File(target+Filename); //copy file from one location to other FileUtils.copyFile(sourceFile, targetFile); </code></pre> <p><strong>Question :</strong> I am trying now to import a :</p> <ul> <li>2)a whole directory(folder) of images, lets say from C:\importImages into C:\output .</li> </ul> <p><strong>Answer:</strong></p> <p><strong>What is the standard way nowadays to accomplish directory iteration with java?</strong></p> <p><a href="https://stackoverflow.com/questions/3154488/best-way-to-iterate-through-a-directory-in-java">Best way to iterate through a directory in java?</a></p> <p>Add these ways in your code. Is not that hard.</p> <ul> <li>Test for the file extension (.jpg .gif ...)</li> <li>Don't forget to catch</li> <li>NullPointerException - if source or destination is null</li> <li>IOException - if source or destination is invalid</li> <li>IOException - if an IO error occurs during copying</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload