Java File Copy nio copyFileByMapped(String sourcePath, String targetPath)

Here you can find the source of copyFileByMapped(String sourcePath, String targetPath)

Description

copy File By Mapped

License

Open Source License

Declaration

public static final void copyFileByMapped(String sourcePath, String targetPath) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {

    public static final void copyFileByMapped(String sourcePath, String targetPath) throws IOException {
        RandomAccessFile readFile = new RandomAccessFile(sourcePath, "r");
        RandomAccessFile writeFile = new RandomAccessFile(targetPath, "rw");

        long fileLength = readFile.length();

        MappedByteBuffer in = readFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileLength);
        MappedByteBuffer out = writeFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, fileLength);

        for (int i = 0; i < fileLength; i++) {
            out.put(in.get());/*from  www .  ja va 2s. c  om*/
        }

        readFile.close();
        writeFile.close();
        in.clear();
        out.clear();
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(FileChannel srcFc, File dstFile)
  3. copyFile(final File source, final File dest)
  4. copyFile(final File source, final File target)
  5. copyFile(final File source, final File target)
  6. copyToFile(final InputStream is, final File file)
  7. copyToTempFile(final InputStream is, final String prefix, final String suffix)
  8. copyToTmpFile(InputStream in, String prefix, String suffix)
  9. fileCopy(File source, File destination)