Home > Java, Java 1.6 Tutorial > Moving file or directory to other directory in Java

Moving file or directory to other directory in Java


We moved. Please visit this link for this post.

 

 

 

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;

/**

* This class represents how need to move file or directory to other directory

* @author FaYna Soft Labs

*/

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, you moved file/directory.

Advertisements
Categories: Java, Java 1.6 Tutorial
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: