Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file from directory to another Directory in Java

Tags:

java

I am using JDK 6.

I have 2 folders names are Folder1 and Folder2.

Folder1 have the following files

TherMap.txt

TherMap1.txt

TherMap2.txt

every time Folder2 have only one file with name as TherMap.txt.

What I want,

copy any file from folder1 and pasted in Folder2 with name as TherMap.txt.If already TherMap.txt exists in Folder2, then delete and paste it.

for I wrote the following code.but it's not working

public void FileMoving(String sourceFilePath, String destinationPath, String fileName) throws IOException {
    File destinationPathObject = new File(destinationPath);
    File sourceFilePathObject = new File(sourceFilePath);
    if ((destinationPathObject.isDirectory()) && (sourceFilePathObject.isFile()))
    //both source and destination paths are available 
    {
        //creating object for File class
        File statusFileNameObject = new File(destinationPath + "/" + fileName);
        if (statusFileNameObject.isFile())
        //Already file is exists in Destination path
        {
            //deleted File
            statusFileNameObject.delete();
            //paste file from source to Destination path with fileName as value of fileName argument
            FileUtils.copyFile(sourceFilePathObject, statusFileNameObject);
        }
        //File is not exists in Destination path.
        {
            //paste file from source to Destination path with fileName as value of fileName argument
            FileUtils.copyFile(sourceFilePathObject, statusFileNameObject);
        }
    }
}

I call the above function in main()

 //ExternalFileExecutionsObject is class object
 ExternalFileExecutionsObject.FileMoving(
            "C:/Documents and Settings/mahesh/Desktop/InputFiles/TMapInput1.txt",
            "C:/Documents and Settings/mahesh/Desktop/Rods",
            "TMapInput.txt");

While I am using FileUtils function, it showing error so I click on error, automatically new package was generated with the following code.

 package org.apache.commons.io;
 import java.io.File;
 public class FileUtils {
    public static void copyFile(File sourceFilePathObject,
        File statusFileNameObject) {
        // TODO Auto-generated method stub
    }
 }

my code not showing any errors,even it's not working.

How can I fix this.

Thanks

like image 457
ur truly friend Avatar asked Jul 23 '13 14:07

ur truly friend


People also ask

How do you copy a file from one folder to another?

Copy and paste files Select the file you want to copy by clicking on it once. Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V .

How do you copy and paste a file in Java?

Here is the method that can be used to copy a file using FileChannel. Apache Commons IO FileUtils. copyFile(File srcFile, File destFile) can be used to copy file in java. If you are already using Apache Commons IO in your project, it makes sense to use this for code simplicity.

How do I save a file to a specific directory in Java?

Try something like this: File file = new File("/some/absolute/path/myfile. ext"); OutputStream out = new FileOutputStream(file); // Write your data out. close();

How do I change the directory of a file in Java?

Creating a new directoryThe mkdir() method of this class creates a directory with the path represented by the current object. Instantiate the File class by passing the path of the directory you need to create, as a parameter (String). Invoke the mkdir() method using the above created file object.


1 Answers

Use Apache Commons FileUtils FileUtils.copyDirectory(source, desc);

like image 54
Charu Khurana Avatar answered Sep 21 '22 23:09

Charu Khurana