Moving file or directory to other directory in Java

In Wallpaper Changer application version 1.2.* we added feature which allows to move file in application’s file manager.

I want to show how we did it. As you see this post refer to core Java not Android. That’s why I will post code with method main.

Example is simple,just copy it and use.

import java.io.File;public class Main{ public static void main(String[] args){ // File (or directory) to be moved  File file = new File("C:/sampleDir/sample.data"); // Destination directory  File dir = new File("C:/sampleDir/otherDir"); // Move file to new directory  boolean success = file.renameTo(new File(dir,file.getName())); if (!success){ System.err.println("Error"); }  }}

That’s all,we moved file/directory.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.