Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Moving file or directory from one directory to another

Related Tutorials


 
 

Share on Google+Share on Google+

Moving file or directory from one directory to another

Advertisement
In this section, you will learn how the contents of a file or a complete directory is moved from one directory to another directory.

Moving file or directory from one directory to another

     

Introduction

In this section, you will learn how  the contents of a file or a complete directory is moved from one directory to another directory. This program illustrates you the method or procedure for moving a file or directory contents from one directory to another. In this section an example is given for the best illustration of the procedure through which you can easily move contents of the mention file or directory as a source directory to the destination directory. If the mentioned source file or directory does not exist then the following program gives you a message "File or directory does not exist." then the control quits from the program. If the mentioned destination directory does not exist then the program asks you for the creation of the directory with the given name and if you enter "y", it will create a new directory and copy all the files and folders contained in the source directory. This program also asks for the replace folder or file if the mentioned destination file or directory already exists.

For running the program properly you must have to mention the complete path for the source or the destination from/to the files or the folders have to be moved. In the following program there are three methods have been used excepting the main method to complete the program for moving file or complete folder from specified source if exists to destination. These methods are explained one-by-one in brief as follows:

copyDirectory(File sourceDir, File destDir):
This the user define method which is used for the copying directory or folders where it found. This method passes two arguments in which one is the source directory name and another is the destination directory name. Both directory name must must be in the File type format.

copyFile(File source, File dest):
This is also a user defined method i.e. used for copying files from the source directory to the destination directory. This method passes two types of arguments in which one is the source file name and another is the destination file name and both file name must has to be mentioned in the File type format.

delete(File resource):
This is the method which has created in the following program for deleting all the files or the folders that have to be moved from the source to destination. This method starts deletion of files and folders after copying these to the destination directory. This method passes the resource file name which has to be deleted after moving.

import java.io.*;
import javax.swing.*;

public class MovingFile{
public static void main(String[] args) throws IOException{

int a = 0;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
System.out.print(
"Enter the file or directory name that has to be moved : ");

String src = in.readLine();

if(src.equals("")){

System.out.println("Invalid directory or file name.");

System.exit(0);

}

File source = new File(src);

if(!source.exists()){

System.out.println("File or directory does not exist.");

System.exit(0);

}

System.out.print(
"Enter the complete path where file or directory has to be 
moved: ");

String dest = in.readLine();

if(dest.equals("")){

System.out.println("Invalid directory or file name.");

System.exit(0);

}

File destination = new File(dest);



if(!destination.exists()){

System.out.print(
"Mentioned directory does not exist. \nDo you want to 
create a new directory(Y/N)? ");

String chk = in.readLine();



if(chk.equals("Y") || chk.equals("y")){

destination.mkdir();

copyDirectory(source, destination);

a = 1;

}

else if(chk.equals("N") || chk.equals("n")){

System.exit(0);

}

else{

System.out.println("Invalid Entry!");

System.exit(0);

}

}

else{

int num = JOptionPane.showConfirmDialog(null, 
"Given file or folder name already exists. \nDo you want to replace 
now?");

if(num == 0){

copyDirectory(source, destination);

a = 1;

}

}

if(a == 1){

System.out.println("File or directory moved successfully.");

if(!delete(source)){

throw new IOException("Unable to delete original folder");

}

} else if(a == 0){

System.exit(0);

}

}
public static void copyDirectory(File sourceDir, File destDir) throws 
IOException{

if(!destDir.exists()){

destDir.mkdir();

}

File[] children = sourceDir.listFiles();

for(File sourceChild : children){

String name = sourceChild.getName();

File destChild = new File(destDir, name);

if(sourceChild.isDirectory()){

copyDirectory(sourceChild, destChild);

}

else{

copyFile(sourceChild, destChild);

}

}

}
public static void copyFile(File source, File dest) throws IOException{

if(!dest.exists()){

dest.createNewFile();

}

InputStream in = null;

OutputStream out = null;

try{

in = new FileInputStream(source);

out = new FileOutputStream(dest);

byte[] buf = new byte[1024];

int len;

while((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

}

finally{

in.close();

out.close();

}

}
public static boolean delete(File resource) throws IOException{ 

if(resource.isDirectory()){

File[] childFiles = resource.listFiles();

for(File child : childFiles){

delete(child);

}

}

return resource.delete();

}

}

Here is the code of the program : 

Output: Here "test" is the root folder and "source" directory already exist in this folder. If you want to move "source" into "destination". Please run program this way...


C:\work\chandan>javac MovingFile.java

C:\work\chandan>java MovingFile
Enter the file or directory name that has to be moved : E:\source
Enter the complete path where file or directory has to moved: c:\work\chandan\destination
Mentioned directory does not exist.
Do you want to create a new directory(Y/N)? y
File or directory moved successfully.

C:\work\chandan>

If you want to move file or directory from other directory .Please enter qualified path of  "source" and destination and run this program this way... 

Download this example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Discuss: Moving file or directory from one directory to another   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Alex Furman
July 2, 2011
This can be done with a one-liner

Moving a file into another dir is a one-liner. See java.io.File#renameTo Doing what you're doing here, actually copying the content and then deleting the original files is insanity, pure and simple. I would remove this embarassing attempt at a "tutorial" if I were you.
pradip garala
May 2, 2012
renameTo

we can do like this way..... File file = new File("C:\\A\\file.txt"); if(file.renameTo(new File("C:\\B\\" + file.getName()))){ System.out.println("File is moved successful!"); }else{ System.out.println("File is failed to move!"); }
raj
August 9, 2012
Date folder Creation

Take the file from one folder and move it to new folder which created by Date ? can some one help me.
DMCA.com