java files

Java copy file to another path

public static Path copy(Path source,Path target,CopyOption… options)
throws IOException

Copy a file to a target file.
This method copies a file to the target file with the options parameter specifying how the copy is performed. By default, the copy fails if the target file already exists or is a symbolic link, except if the source and target are the same file, in which case the method completes without copying the file. File attributes are not required to be copied to the target file. If symbolic links are supported, and the file is a symbolic link, then the final target of the link is copied. If the file is a directory then it creates an empty directory in the target location (entries in the directory are not copied). This method can be used with the walkFileTree method to copy a directory and all entries in the directory, or an entire file-tree where required.
Parameters:
source – the path to the file to copy
target – the path to the target file (may be associated with a different provider to the source path)
options – options specifying how the copy should be done
Returns:
the path to the target file
Throws:
UnsupportedOperationException – if the array contains a copy option that is not supported
FileAlreadyExistsException – if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not specified (optional specific exception)
DirectoryNotEmptyException – the REPLACE_EXISTING option is specified but the file cannot be replaced because it is a non-empty directory (optional specific exception)
IOException – if an I/O error occurs
SecurityException – In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the source file, the checkWrite is invoked to check write access to the target file. If a symbolic link is copied the security manager is invoked to check LinkPermission(“symbolic”).

package com.candidjava.filenio;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileCopy {
  public static void main(String[] args) {
    Path source=Paths.get("F:/Filesnio/copy.txt");
    Path destination=Paths.get("F:/Filesnio1/copy1.txt");
    try {
      Files.copy(source, destination);
      System.out.println("File copied sucessfully");
    } catch (IOException e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }

}

Old Approach

package com.candidjava.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy {
  public static void main(String[] args) {

    try {
      File infile = new File("F:/Files/inputfile.txt");
      File outfile = new File("F:/Files/outputfile.txt");
      FileInputStream fi = new FileInputStream(infile);
      FileOutputStream fo = new FileOutputStream(outfile);
      byte b[] = new byte[1024];
      int length;
      while ((length = fi.read(b)) > 0) {
        fo.write(b);

      }
      System.out.println("File Copied Successfully");
      fo.close();
      fi.close();
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
}