Java File Copy copyFileFromFileSystem(String sourceFileName, File target)

Here you can find the source of copyFileFromFileSystem(String sourceFileName, File target)

Description

copy File From File System

License

Apache License

Declaration

private static void copyFileFromFileSystem(String sourceFileName, File target) 

Method Source Code

//package com.java2s;
/**/*from  w w  w .  j  ava 2  s.com*/
 * Copyright 2012 msg systems ag
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import javax.swing.*;
import java.io.*;
import java.nio.channels.FileChannel;

public class Main {
    private static void copyFileFromFileSystem(String sourceFileName, File target) {
        try {
            File in = new File(sourceFileName);
            FileChannel inChannel = new FileInputStream(in).getChannel();
            FileChannel outChannel = new FileOutputStream(target).getChannel();
            try {
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } catch (IOException e) {
                throw e;
            } finally {
                if (inChannel != null)
                    inChannel.close();
                if (outChannel != null)
                    outChannel.close();
            }
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, e);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

Related

  1. copy(File from, File to)
  2. copyFile(File source, File target)
  3. copyFile(File src, File dest, Component parentComponent, String message)
  4. copyFiles(File file, File destPath)
  5. copyFiles(File fromDir, File toDir)
  6. copyFiles(File source, File destination)
  7. copyFiles(File sourceFile, File targetDir)