Java Utililty Methods FileChannel Copy

List of utility methods to do FileChannel Copy

Description

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

Method

booleancopyFile(final String src, final String dst)
Copies source file content into destination file.
try {
    final File dstDir = new File(new File(dst).getParent());
    if (ensureDirectoryExists(dstDir)) {
        final FileChannel srcFC = new FileInputStream(src).getChannel();
        final FileChannel dstFC = new FileOutputStream(dst).getChannel();
        return copyFile(srcFC, dstFC);
    } else {
        return false;
...
voidcopyFile(InputStream is, OutputStream os)
copy File
byte[] buf = new byte[BUF_SIZE];
int len;
while ((len = is.read(buf)) > 0) {
    os.write(buf, 0, len);
is.close();
os.close();
voidcopyFile(java.io.File fromFile, java.io.File toFile)
Copy one file to another.
FileChannel toChannel;
try (FileChannel fromChannel = new FileInputStream(fromFile).getChannel()) {
    toChannel = new FileOutputStream(toFile).getChannel();
    toChannel.transferFrom(fromChannel, 0L, fromChannel.size());
toChannel.close();
voidcopyFile(java.io.File in, java.io.File out)
copy File
createParentDirectoriesIfNecessary(out);
java.nio.channels.FileChannel inChannel = new java.io.FileInputStream(in).getChannel();
java.nio.channels.FileChannel outChannel = new java.io.FileOutputStream(out).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
voidcopyFile(Path source, Path target)
Copy a file and try to guarantee the copy is on disk.
try (FileChannel in = FileChannel.open(source, READ)) {
    long size = in.size();
    try (FileChannel out = FileChannel.open(target, WRITE, CREATE_NEW)) {
        long position = 0;
        while (position < size) {
            position += in.transferTo(position, size - position, out);
        out.force(false);
...
booleancopyFile(String fromFileName, String toFileName)
copy File
return copyFile(new File(fromFileName), new File(toFileName));
booleancopyFile(String fromPath, String toPath)
Copy the file.
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
    fromChannel = new FileInputStream(fromPath).getChannel();
    toChannel = new FileOutputStream(toPath).getChannel();
    fromChannel.transferTo(0, fromChannel.size(), toChannel);
} catch (IOException e) {
    return false;
...
booleancopyFile(String in, String out)
copy File
File inFile = new File(in);
File outFile = new File(out);
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
    inChannel = new FileInputStream(inFile).getChannel();
    outChannel = new FileOutputStream(outFile).getChannel();
    int maxCount = (64 * 1024 * 1024) - (32 * 1024);
...
voidcopyFile(String infile, String outfile)
copy File
try {
    FileInputStream fin = new FileInputStream(infile);
    FileOutputStream fout = new FileOutputStream(outfile);
    FileChannel fcin = fin.getChannel();
    FileChannel fcout = fout.getChannel();
    try {
        fcout.transferFrom(fcin, 0, fcin.size());
    } finally {
...
voidcopyFile(String inFile, String outFile)
copy File
File in = new File(inFile);
File out = new File(outFile);
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
    inChannel = new FileInputStream(in).getChannel();
    outChannel = new FileOutputStream(out).getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
...