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

voidcopyFile(File f1, File f2)
Copy file
if (f1 == null || f2 == null || !f1.exists()) {
    return;
int length = 2097152;
FileInputStream in = new FileInputStream(f1);
FileOutputStream out = new FileOutputStream(f2);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
...
voidcopyFile(File source, File dest, boolean visibleFilesOnly)
Copy a file from source to dest.
if (visibleFilesOnly && isHiddenOrDotFile(source)) {
    return;
if (dest.exists()) {
    System.err.println("Destination File Already Exists: " + dest);
FileChannel in = null, out = null;
try {
...
voidcopyFile(File src, File dest)
copy File
FileInputStream input = null;
FileChannel inputChannel = null;
FileOutputStream output = null;
FileChannel outputChannel = null;
try {
    input = new FileInputStream(src);
    inputChannel = input.getChannel();
    output = new FileOutputStream(dest);
...
longcopyFile(FileChannel srcFc, File dstFile)
copy File
long r = 0;
FileOutputStream fos = new FileOutputStream(dstFile);
try {
    ByteBuffer bb = ByteBuffer.allocate(32768);
    for (int n; (n = srcFc.read(bb)) != -1;) {
        bb.flip();
        fos.write(bb.array(), 0, bb.limit());
        bb.clear();
...
voidcopyFile(final File source, final File dest)
Copia un fichero.
if (source == null || dest == null) {
    throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); 
final FileInputStream is = new FileInputStream(source);
final FileOutputStream os = new FileOutputStream(dest);
final FileChannel in = is.getChannel();
final FileChannel out = os.getChannel();
final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
...
voidcopyFile(final File source, final File target)
Copy a file from source to target.
FileChannel in = new FileInputStream(source).getChannel(), out = new FileOutputStream(target).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (in.read(buffer) != -1) {
    buffer.flip(); 
    out.write(buffer);
    buffer.clear(); 
out.close();
...
voidcopyFile(final File source, final File target)
copy File
FileChannel in = null;
FileChannel out = null;
try {
    in = new FileInputStream(source).getChannel();
    out = new FileOutputStream(target).getChannel();
    ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER);
    int readSize = in.read(buffer);
    long totalRead = 0;
...
voidcopyFileByMapped(String sourcePath, String targetPath)
copy File By Mapped
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());
readFile.close();
writeFile.close();
in.clear();
out.clear();
FilecopyToFile(final InputStream is, final File file)
Copies the stream to the temporary file.
final FileOutputStream fos = new FileOutputStream(file);
ReadableByteChannel source = null;
FileChannel target = null;
try {
    source = Channels.newChannel(is);
    target = fos.getChannel();
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (source.read(buffer) != -1) {
...
FilecopyToTempFile(final InputStream is, final String prefix, final String suffix)
Copies the stream to the temporary file.
return copyToFile(is, File.createTempFile(prefix, suffix));