meenu
meenu

Reputation: 3

Java File Copy -How to Backup/copy the input file to other destination location?

Want to copy/backup the file to destination folder before performing any task. (jdk -1.7)


 /*Input file path taken from properties file as string is :inputFile
where-in inputFile is :C:\\Project\\input\\filename.txt
Destination file path taken from properties file as string is : 
archiveFolderPath */

  //Existing code : in main
if (inputFile != null) {
readTextFile(new File(inputFile)); }

// in readTextFile method
BufferedReader br = new BufferedReader(new FileReader(filename));

I tried using the below procedure:: but getting an error: Error:: The method copy(InputStream, OutputStream) in the type Files is not applicable for the arguments (String, String)

//Calling method in main::
copyFiles(inputFile, archiveFolderPath);


//Copy method :
private static void copyFiles (String inputFile, String 
  archiveFolderPath) throws IOException {
    Files.copy(inputFile, archiveFolderPath); }

please suggest an Alternative solution as"Files is not applicable for the arguments (String, String)" .

Upvotes: 0

Views: 849

Answers (1)

Awan Biru
Awan Biru

Reputation: 490

You could copy the file before performing read or write operations on them. Example:-

Path origin = Paths.get("/home/fm/source.txt");
Path destination = Paths.get("/home/fm/source.bak");

//Copy source.txt to source.bak
Files.copy(origin, destination, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);

Refer to Files javadoc for all copy methods detail. Some of them expecting CopyOption as arguments. Choose suitable CopyOption according to program requirements.

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html https://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html#COPY_ATTRIBUTES

Upvotes: 2

Related Questions