Java FileChannel Copy copyToTempDirectory(final String file)

Here you can find the source of copyToTempDirectory(final String file)

Description

copy To Temp Directory

License

Open Source License

Parameter

Parameter Description
file a parameter

Declaration

private static String copyToTempDirectory(final String file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 InfAI.org/*from  w w w  .jav  a  2  s  . c  o m*/
 *
 * 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
 * 
 *******************************************************************************/

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * @param file
     * @return
     */
    private static String copyToTempDirectory(final String file) {
        try {
            final String ending = file.substring(file.lastIndexOf('.'));
            final File tempfile = File.createTempFile("model", ending);
            copyFile(new File(file), tempfile);
            return tempfile.getAbsolutePath();
        } catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static void copyFile(final File in, final File out) throws IOException {
        final FileChannel inChannel = new FileInputStream(in).getChannel();
        final FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (final IOException e) {
            throw e;
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }
}

Related

  1. copyStreamToFile(InputStream inStream, File file)
  2. copyThrowsOnException(final File source, final File destination)
  3. copyTo(File srcFile, File destFile)
  4. copyToFolderAs(File fromFile, File folder, String asName)
  5. copyToTemp(File srcFile)
  6. copyTransfer(String srcPath, String destPath)
  7. copyTree(File sourceLocation, File targetLocation)
  8. copyTree(File srcFile, File trgFile)
  9. copyTree(final File source, final File dest)