Android File Copy copy(String srcFileName, String destFileName)

Here you can find the source of copy(String srcFileName, String destFileName)

Description

This may be a resource leak: http://bugs.sun.com/view_bug.do?bug_id=4724038 We may have to reconsider using nio for this, or apply one of the horrible workarounds listed in the bug report above.

License

Open Source License

Declaration

public static void copy(String srcFileName, String destFileName)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w w w .  j  av  a  2  s . c  o m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

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

public class Main {
    /**
     * This may be a resource leak:
     * http://bugs.sun.com/view_bug.do?bug_id=4724038
     * 
     * We may have to reconsider using nio for this, or apply one of the horrible
     * workarounds listed in the bug report above.
     */
    public static void copy(String srcFileName, String destFileName)
            throws IOException {
        if (srcFileName == null) {
            throw new IllegalArgumentException("srcFileName is null");
        }
        if (destFileName == null) {
            throw new IllegalArgumentException("destFileName is null");
        }
        FileChannel src = null;
        FileChannel dest = null;
        try {
            src = new FileInputStream(srcFileName).getChannel();
            dest = new FileOutputStream(destFileName).getChannel();
            long n = src.size();
            MappedByteBuffer buf = src.map(FileChannel.MapMode.READ_ONLY,
                    0, n);
            dest.write(buf);
        } finally {
            if (dest != null) {
                try {
                    dest.close();
                } catch (IOException e1) {
                }
            }
            if (src != null) {
                try {
                    src.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}

Related

  1. copy(File sourceFile, String destinction)
  2. copy(File src, File dest)
  3. copy(String input, String output)
  4. copy(String pSourceFile, String pTargetFile)
  5. copy(String source, String destinction)
  6. copyBundleFile(IPath projectRelativePath, String outputPath)
  7. copyFile(File fin, File fout)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to, boolean append)