Java FileInputStream Copy copyFileToDir(File oldFile, File outputDir)

Here you can find the source of copyFileToDir(File oldFile, File outputDir)

Description

Copies a file into a specified directory

License

Open Source License

Parameter

Parameter Description
oldFile the file to copy
outputDir the destination directory

Exception

Parameter Description
IOException if there are IO errors

Declaration

public static void copyFileToDir(File oldFile, File outputDir) throws IOException 

Method Source Code

//package com.java2s;
/*==========================================================================*\
 |  $Id: FileUtilities.java,v 1.5 2014/06/16 15:59:40 stedwar2 Exp $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2006-2012 Virginia Tech
 |
 |  This file is part of Web-CAT.//w ww  .  java2 s.c o m
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU Affero General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU Affero General Public License
 |  along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copies a file into a specified directory
     *
     * @param oldFile the file to copy
     * @param outputDir the destination directory
     * @throws IOException if there are IO errors
     */
    public static void copyFileToDir(File oldFile, File outputDir) throws IOException {
        FileInputStream in = new FileInputStream(oldFile);
        File destFile = new File(outputDir, oldFile.getName());
        FileOutputStream out = new FileOutputStream(destFile);
        copyStream(in, out);
        in.close();
        out.close();
        destFile.setLastModified(oldFile.lastModified());
    }

    /**
     * Copies the contents of an input stream onto an existing output
     * stream.  The output stream is flushed when the operation
     * is complete.
     *
     * @param in  the input stream to copy
     * @param out the destination
     * @throws IOException if there are IO errors
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        final int BUFFER_SIZE = 65536;

        // read in increments of BUFFER_SIZE
        byte[] b = new byte[BUFFER_SIZE];
        int count = in.read(b);
        while (count > -1) {
            out.write(b, 0, count);
            count = in.read(b);
        }
        out.flush();
    }
}

Related

  1. copyFileR(File srcFile, File destDirectory)
  2. copyFileRecursive(File src, File dest)
  3. copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
  4. copyFileToContainer(File pChild, IContainer pContainer)
  5. copyFileToDestDir(String srcFilePath, String destFileDir)
  6. copyFileToDir(File sourceFile, File destDir)
  7. copyFileToDir(String targetDir, String[] filePath)
  8. CopyFileToDirectory(String file_name, String from_directory, String to_directory)
  9. copyFileToFile(File file, File destFile, boolean overwrite)