Noah
Noah

Reputation: 109

Java program that program CopyFile that copies one file to another

I want to write a program that copies one file to another. I got my program to execute and run but nothing happens! I have no errors to go by so I'm stuck and don't know what to do! It doesn't create the files or copies them into one file.

Here's the command I typed:

java CopyFile report.txt report.sav

The program should create another copy of the file report.txt in report.sav. Your program should print the following error message for inappropriate number of input arguments (for e.g., java CopyFile report.txt):

Here's my code:

import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

/**
   This program copies one file to another.
*/
public class CopyFile
{  
   public static void main(String[] args) throws FileNotFoundException
   {  
      if (args.length != 2)
      {  
         System.out.println("Usage: java CopyFile fromFile toFile");
         return;
      }
      String source = args[0];

   }
}

Upvotes: 1

Views: 151

Answers (2)

RajSharma
RajSharma

Reputation: 1971

use this-

Files.copy(source.toPath(), dest.toPath());

This method you can find in java 7.

Refer to this link for other ways- http://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/

Upvotes: 1

DDan
DDan

Reputation: 8276

You can use FileUtils from Apache IOCommons

FileUtils.copyFile(src, dest)

Upvotes: 0

Related Questions