Home > Java, Java 1.6 Tutorial > Renaming file or directory in Java

Renaming file or directory in Java


We moved. Please visit this link for this post.

 

 

 

I am going to show how to rename file or directory in Java.

import java.io.File;

/**
 * This class shows how to rename file or directory
 * @author FaYna Soft Labs
 */
public class Main {

    public static void main(String[] args) {
        // File (or directory) with old name
        File oldFile = new File("C:/sampleDir");

        // File (or directory) with new name
        File newFile = new File("C:/sample");

        // Rename file (or directory)
        boolean success = oldFile.renameTo(newFile);
        if (!success) {
            System.err.println("Error");
        }
    }
}

You can copy this example to your IDE and use it for your objective

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: