Example usage for java.nio.channels FileChannel transferTo

List of usage examples for java.nio.channels FileChannel transferTo

Introduction

In this page you can find the example usage for java.nio.channels FileChannel transferTo.

Prototype

public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;

Source Link

Document

Transfers bytes from this channel's file to the given writable byte channel.

Usage

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {/*w w  w. java  2 s  .c o  m*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {/*from  w w w.  j  av a2  s  . c  o  m*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
            outChannel.close();
        }
    }
}

From source file:Main.java

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {// w ww .  java2s .  com
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

From source file:Main.java

/**
 * Copies the contents of the file in {@code src} to {@code dst} and then deletes the {@code src} if copy was successful.
 * If the file copy was unsuccessful, the src file will not be deleted.
 * @param src Source file//from  w w  w .j a v  a2s  . c  o m
 * @param dst Destination file
 * @throws IOException if an error occurred during the file copy
 */
static void moveFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        long bytesCopied = inChannel.transferTo(0, inChannel.size(), outChannel);
        if (bytesCopied >= src.length())
            src.delete();
    } finally {
        if (inChannel != null)
            inChannel.close();
        outChannel.close();
    }
}

From source file:Main.java

public static void copyFile(@NonNull String pathFrom, @NonNull String pathTo) throws IOException {
    FileChannel outputChannel = null;
    FileChannel inputChannel = null;
    try {/*ww  w. ja v  a2s  .c o m*/
        inputChannel = new FileInputStream(new File(pathFrom)).getChannel();
        outputChannel = new FileOutputStream(new File(pathTo)).getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        inputChannel.close();
    } finally {
        if (inputChannel != null)
            inputChannel.close();
        if (outputChannel != null)
            outputChannel.close();
    }

}

From source file:Main.java

public static void copyFileFast(File in, File out) {
    FileChannel filein = null;
    FileChannel fileout = null;//ww  w  . j  a  v  a2  s  . co m
    try {
        filein = new FileInputStream(in).getChannel();
        fileout = new FileOutputStream(out).getChannel();
        filein.transferTo(0, filein.size(), fileout);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(filein, fileout);
    }
}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void sendFileTest(File source, File target) throws Exception {
    FileInputStream fis = null;/* ww w. j a  va  2 s.com*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        FileChannel sChannel = fis.getChannel();
        FileChannel tChannel = fos.getChannel();
        target.createNewFile();
        sChannel.transferTo(0, source.length(), tChannel);
        tChannel.close();
        sChannel.close();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.cats.version.utils.Utils.java

public static void copyFile(File srcFile, File destFile) {
    FileInputStream fis = null;//from   w  w w.j a  v  a2s  .c o  m
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        Utils.closeRes(fis);
        Utils.closeRes(fos);
    }
}

From source file:VASSAL.tools.io.IOUtils.java

/**
 * Copies bytes from a large (over 2GB) <code>FileInputStream</code> to a
 * <code>FileOutputStream</code>.
 *
 * This method uses channels. The input file should not be written
 * to during the copy./*from   w  ww . jav  a 2 s  .  c  o m*/
 *
 * @param in the source
 * @param out the destination
 * @throws IOException if one occurs while reading or writing
 */
public static long copyLarge(FileInputStream in, FileOutputStream out) throws IOException {
    final FileChannel inc = in.getChannel();
    return inc.transferTo(0L, inc.size(), out.getChannel());
}

From source file:com.intuit.tank.service.impl.v1.report.FileReader.java

/**
 * Gets a StreamingOutput from the passedin file from start to end or from beginning to end if start is greater than
 * end. if a negative number is passed, it will get the last n lines of the file. If 0 is passed it will return the
 * entire file./*w  ww. j  a v a2s .c o  m*/
 * 
 * @param total
 * 
 * @return a StreamingOutput
 */
public static StreamingOutput getFileStreamingOutput(final File f, long total, String start) {

    long l = 0;
    if (start != null) {
        try {
            l = Long.parseLong(start);
            // num lines to get from end
            if (l < 0) {
                l = getStartChar(f, Math.abs(l), total);
            }
        } catch (Exception e) {
            LOG.error("Error parsing start " + start + ": " + e);
        }
    }
    final long to = l > total ? 0 : total;
    final long from = l;

    StreamingOutput streamer = new StreamingOutput() {
        @SuppressWarnings("resource")
        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {

            final FileChannel inputChannel = new FileInputStream(f).getChannel();
            final WritableByteChannel outputChannel = Channels.newChannel(output);
            try {
                inputChannel.transferTo(from, to, outputChannel);
            } finally {
                // closing the channels
                inputChannel.close();
                outputChannel.close();
            }
        }
    };
    LOG.debug("returning data from " + from + " - " + to + " of total " + total);
    return streamer;
}