Vidya
Vidya

Reputation: 21

Unable to Move File from one location to another location on linux file system using java

I am trying to move files from one location to another location in Linux file system. Actually my source directory is on one file system and destination directory is mounted on to the same file system. So am using File.renameTo() method of File class to move the files. But it is failing to move the files. But when i use the same logic to move the files from one directory to another directory that are mounted on same file system, it is working fine.. So i am thinking file.renameTo() is platform dependent. So am using other classes to move the files. So, Now my choice is to go for org.apache.commons.io.FileUtils class. It contains method such as,

public static void moveFile(File srcFile,File destFile) throws IOException

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#moveFile%28java.io.File,%20java.io.File%29

So i downloaded commons-io-1.3 version jar. It contains so many methods to copy file but am unable to find this moveFile method. Please can any one tell me whether i downloaded the correct jar file ?

Can any one please tell me the jar that contains org.apache.commons.io.FileUtils.(File srcFile,File destFile) method.

Thanks in Advance.

Upvotes: 0

Views: 5068

Answers (3)

Harsimranjit Singh Kler
Harsimranjit Singh Kler

Reputation: 2302

May be helpful for some one

Path pathobj1 = Paths.get(srcDir,filename);
Path pathobj2= Paths.get(targetDir,pathobj1.getFileName().toString());
Files.move(pathobj1 , pathobj2, REPLACE_EXISTING);

Package:

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136012

Java 1.7 has a more reliable way of moving files, try it

java.nio.file.Files.move(Path source, Path target, CopyOption... options)

Upvotes: 2

bmargulies
bmargulies

Reputation: 100050

The javadoc clearly says '1.4'.

Since:
1.4

So you won't find it in 1.3.

Upvotes: 2

Related Questions