Java Utililty Methods File Copy nio

List of utility methods to do File Copy nio

Description

The list of methods to do File Copy nio are organized into topic(s).

Method

voidcopy(File src, File dst)
copy
Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING);
booleancopy(File src, File dst)
copy
boolean success = true;
FileChannel srcChannel = null, dstChannel = null;
try {
    srcChannel = new FileInputStream(src).getChannel();
    dstChannel = new FileOutputStream(dst).getChannel();
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
} catch (IOException e) {
    success = false;
...
voidcopy(File src, File folder)
copy the file src to this folder
try {
    File dst = new File(folder, src.getName());
    Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
    e.printStackTrace();
longcopy(File src, File target)
copy
RandomAccessFile in = null;
RandomAccessFile out = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
    in = new RandomAccessFile(src, "r");
    target.getParentFile().mkdirs();
    out = new RandomAccessFile(target, "rw");
...
voidcopy(File src, File target)
copy
FileInputStream fin = null;
FileOutputStream fout = null;
try {
    fin = new FileInputStream(src);
    fout = new FileOutputStream(target);
    WritableByteChannel outChannel = Channels.newChannel(fout);
    fin.getChannel().transferTo(0, fin.available(), outChannel);
} finally {
...
voidcopy(File srcDir, File relSrcFile, File destDir, File relDestFile)
Copy the specified source file given relative to the specified source folder to the specified destination file relative to the specified destination folder
File finalSrcFile = (srcDir != null) ? new File(srcDir, relSrcFile.getPath()) : relSrcFile;
File relDestDir = relDestFile.getParentFile();
if (relDestDir != null) {
    File finalDestDir = (destDir != null) ? new File(destDir, relDestDir.getPath()) : relDestDir;
    mkdirs(finalDestDir);
File finalDestFile = new File(destDir, relDestFile.getPath());
Files.copy(finalSrcFile.toPath(), finalDestFile.toPath(), StandardCopyOption.REPLACE_EXISTING,
...
voidcopy(File srcfile, File destfile)
Copy a file or directory from one location to another
if (srcfile.isDirectory()) {
    destfile.mkdirs();
    String[] files = srcfile.list();
    for (int i = 0; i < files.length; i++) {
        String newSrcFile = srcfile.getPath() + File.separator + files[i];
        String newDestFile = destfile.getPath() + File.separator + files[i];
        copy(new File(newSrcFile), new File(newDestFile));
} else if (srcfile.isFile()) {
    FileChannel inputChannel = new FileInputStream(srcfile).getChannel();
    FileChannel outputChannel = new FileOutputStream(destfile).getChannel();
    inputChannel.transferTo(0, inputChannel.size(), outputChannel);
    inputChannel.close();
    outputChannel.close();
} else {
    throw new Exception("Invalid file : " + srcfile);
voidcopy(File srcFile, File dstFile)
copy
if (srcFile.isDirectory()) {
    dstFile.mkdir();
    for (File entry : srcFile.listFiles()) {
        copy(entry, new File(dstFile, entry.getName()));
} else {
    FileInputStream isSrc = null;
    FileOutputStream osDst = null;
...
voidcopy(File srcFileOrDir, File targetFileOrDir)
copy
try {
    copy(srcFileOrDir, targetFileOrDir, false);
} catch (IOException e) {
    e.printStackTrace();
voidcopy(File toFile, File fromFile)
Copies data from one file to another.
FileChannel source = new FileInputStream(fromFile).getChannel();
try {
    FileChannel sink = new FileOutputStream(toFile).getChannel();
    try {
        for (long position = 0, size = source.size(); position < size;)
            position += source.transferTo(position, size - position, sink);
    } finally {
        sink.close();
...