LoginSignup
6
7

More than 5 years have passed since last update.

Java nio対応 ファイルコピー

Last updated at Posted at 2014-06-25

ファイルコピーのJava1.5以降対応版。

FileUtil.java
    /**
     * NIOチャネルを使って高速にコピーします。
     * @param srcPath コピー元パス
     * @param dstPath コピー先パス
     * @throws IOException IOエラー
     */
    public static void copy(final String srcPath, final String dstPath) throws IOException {
        FileChannel srcChannel = new FileInputStream(srcPath).getChannel();
        FileChannel destChannel = new FileOutputStream(dstPath).getChannel();
        try {
            srcChannel.transferTo(0, srcChannel.size(), destChannel);
        } finally {
            srcChannel.close();
            destChannel.close();
        }
    }

    /**
     * NIOチャネルを使って高速にコピーします。
     * @param src コピー元パス
     * @param dst コピー先パス
     * @throws IOException IOエラー
     */
    public static void copy(final File src, final File dst) throws IOException {
        FileChannel srcChannel = new FileInputStream(src).getChannel();
        FileChannel destChannel = new FileOutputStream(dst).getChannel();
        try {
            srcChannel.transferTo(0, srcChannel.size(), destChannel);
        } finally {
            srcChannel.close();
            destChannel.close();
        }
    }

6
7
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7