ur truly friend
ur truly friend

Reputation: 553

How to copy file from directory to another Directory in 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

Upvotes: 6

Views: 26406

Answers (2)

Charu Khurana
Charu Khurana

Reputation: 4551

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

Upvotes: 9

Ted
Ted

Reputation: 3260

Your code isn't working because in order to use the ApacheCommons solution you will have to download the ApacheCommons library found here:

http://commons.apache.org/

and add a reference to it.

Since you are using JRE 6 you can't use all the NIO file utilities, and despite everyone loving Apache Commons as a quick way to answer forum posts, you may not like the idea of having to add that utility on just to get one function. You can also use this code that uses a transferFrom method without using ApacheCommons.

public static void copyFile(File sourceFile, File destFile) throws IOException {
  if (!destFile.exists()) {
    destFile.createNewFile();
  }
  FileInputStream fIn = null;
  FileOutputStream fOut = null;
  FileChannel source = null;
  FileChannel destination = null;
  try {
    fIn = new FileInputStream(sourceFile);
    source = fIn.getChannel();
    fOut = new FileOutputStream(destFile);
    destination = fOut.getChannel();
    long transfered = 0;
    long bytes = source.size();
    while (transfered < bytes) {
      transfered += destination.transferFrom(source, 0, source.size());
      destination.position(transfered);
    }
  } finally {
    if (source != null) {
      source.close();
    } else if (fIn != null) {
      fIn.close();
    }
    if (destination != null) {
      destination.close();
    } else if (fOut != null) {
      fOut.close();
    }
  }
}

When you upgrade to 7, you will be able to do the following

public static void copyFile( File from, File to ) throws IOException {
    Files.copy( from.toPath(), to.toPath() );
}

reference:

https://gist.github.com/mrenouf/889747

Standard concise way to copy a file in Java?

Upvotes: 1

Related Questions