Java FileChannel Copy copyFileToFolder(final String resultFile, final String targetPath)

Here you can find the source of copyFileToFolder(final String resultFile, final String targetPath)

Description

Copies stored memory leak analysis file to given folder.

License

Open Source License

Parameter

Parameter Description
resultFile Existing results file
targetPath Where to save xml file

Return

True if copy success otherwise False

Declaration

public static boolean copyFileToFolder(final String resultFile, final String targetPath) 

Method Source Code

//package com.java2s;
/*//from   w ww .  jav a 2  s  .  c om
 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:  Definitions for the class Util
 *
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copies stored memory leak analysis file to given folder.
     * 
     * @param resultFile
     *            Existing results file
     * @param targetPath
     *            Where to save xml file
     * @return True if copy success otherwise False
     */
    public static boolean copyFileToFolder(final String resultFile, final String targetPath) {
        FileChannel inputChannel = null;
        FileChannel ouputChannel = null;
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        boolean returnValue = true;
        try {

            // get channel to existing file
            inStream = new FileInputStream(resultFile);
            inputChannel = inStream.getChannel();

            // get channel to new file
            outStream = new FileOutputStream(targetPath, false);
            ouputChannel = outStream.getChannel();

            // get existing file size
            final long size = inputChannel.size();

            // position inside the file
            long position = 0;

            // copy file contents if there are data to copy
            while (position < size) {
                position += ouputChannel.transferFrom(inputChannel, position, size - position);
            }

            // close opened channels
            inputChannel.close();
            inStream.close();
            ouputChannel.close();
            outStream.close();
        } catch (FileNotFoundException fnfe) {
            returnValue = false;
        } catch (IOException ioe) {
            returnValue = false;
        } finally {
            try {
                if (inputChannel != null) {
                    inputChannel.close();
                    inputChannel = null;
                }
            } catch (IOException ioe) {
                returnValue = false;
            }

            try {
                if (inStream != null) {
                    inStream.close();
                    inStream = null;
                }
            } catch (IOException ioe) {
                returnValue = false;
            }

            try {
                if (ouputChannel != null) {
                    ouputChannel.close();
                    ouputChannel = null;
                }
            } catch (IOException ioe) {
                returnValue = false;
            }

            try {
                if (outStream != null) {
                    outStream.close();
                    outStream = null;
                }
            } catch (IOException ioe) {
                returnValue = false;
            }

        }

        return returnValue;
    }
}

Related

  1. copyFiles(File source, File dest)
  2. copyFiles(File srcFile, File dstFile, boolean overwrite)
  3. copyFiles(final File fromFile, final File toFile)
  4. copyFileToDir(File file, File destDir)
  5. copyFileToDirectory(String fileFrom, String destinationDirectory)
  6. copyFileToStream(File in, OutputStream out)
  7. copyFileUsingChannel(File source, File dest)
  8. copyFileUsingChannel(File source, File dest)
  9. copyFileUsingFileChannels(File source, File dest)