How to copy a file from a directory to another directory in Java

I am using JDK 6 .

I have two folder Folder1 and Folder2 .

Folder1 have the following files

 TherMap.txt TherMap1.txt TherMap2.txt 

every time Folder2 has only one file named TherMap.txt .

What I want,

copy any file from Folder1 and paste it into Folder2 with a name like TherMap.txt . If TherMap.txt already exists in Folder2 , delete and paste it.

because I wrote the following code. But it does not work

 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 use the FileUtils function, it shows an error, so I click on the error, a new package is automatically created using 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 does not show any errors even if it does not work.

How can I fix this.

thanks

+6
source share
2 answers

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

+8
source

Your code does not work, because to use the ApacheCommons solution you will need to download the ApacheCommons library found here:

http://commons.apache.org/

and add a link to it.

Since you use JRE 6, you cannot use all the NIO file utilities, and although everyone loves Apache Commons as a quick response to forum posts, you might not like the idea of ​​adding this utility just to get one function. You can also use this code that uses the 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 upgrading to 7, you can do the following

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

link:

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

Standard compressed way to copy a file in Java?

+1
source

Source: https://habr.com/ru/post/950087/


All Articles