user2483961
user2483961

Reputation: 11

Moving file into a different directory

I am trying to move the failed files to different directory. Currently, everything seems to work fine except that it creates a file(just plan file without extension). I want directory to be created and all the failed files to go into that directory. Here is my code below. what seems to be wrong?

Path source= Paths.get(("C:/Users/aa/Desktop/whatever" + originalfilename));
Path target = Paths.get("C:/Users/aa/Desktop/Directory1 " );
Files.move(source,target, REPLACE_EXISTING, COPY_ATTRIBUTES);

PS: originalfilename(String) are the filenames of the directory. If I execute it, it gives a file Directory1, but it's not a directory folder.

Upvotes: 1

Views: 123

Answers (2)

Puce
Puce

Reputation: 38152

From the JavaDoc:

 Path source = ...
 Path newdir = ...
 Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move%28java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29

Upvotes: 0

aran
aran

Reputation: 11900

Try this code instead:

File yourFile=new File("D:\\irectory\\Afile.txt");

if(yourFile.renameTo(new File("D:\\irectory\\" + yourFile.getName())))
       System.out.println("File moved succesfully bro!");
else
       System.out.println("Errors moving the file.");

Upvotes: 2

Related Questions