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> |
Download this example
No comments:
Post a Comment