Java FileInputStream Copy copyFile(File src, File toDir)

Here you can find the source of copyFile(File src, File toDir)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(File src, File toDir) throws IOException 

Method Source Code

//package com.java2s;
/*// ww w.j  ava2s. co m
 * Copyright (C) 2014, United States Government, as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All rights reserved.
 *
 * The Java Pathfinder core (jpf-core) platform is 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 java.io.*;

public class Main {
    public static boolean copyFile(File src, File toDir) throws IOException {
        if (src.isFile()) {
            File tgt = new File(toDir, src.getName());
            if (tgt.createNewFile()) {
                byte[] data = getContents(src);
                setContents(tgt, data);
                return true;
            }
        }

        return false;
    }

    public static byte[] getContents(File file) throws IOException {
        if (file.isFile()) {
            long length = file.length();
            byte[] data = new byte[(int) length];

            FileInputStream is = new FileInputStream(file);
            try {
                getContents(is, data);

            } catch (IOException iox) {
                return null;

            } finally {
                is.close();
            }

            return data;
        }

        return null;
    }

    public static void getContents(InputStream is, byte[] buf)
            throws IOException {
        int nRead = 0;
        while (nRead < buf.length) {
            int n = is.read(buf, nRead, buf.length - nRead);
            if (n < 0) {
                throw new IOException("premature end of inputstream: "
                        + buf.length + '/' + nRead);
            }
            nRead += n;
        }
    }

    public static void setContents(File file, byte[] data)
            throws IOException {
        FileOutputStream os = new FileOutputStream(file);
        os.write(data);
        os.close();
    }

    public static void setContents(File file, String data)
            throws IOException {
        FileWriter fw = new FileWriter(file);
        fw.append(data);
        fw.close();
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst, boolean copyLMD)
  5. copyFile(File src, File target)
  6. copyFile(File srcFile, File destFile)
  7. copyFile(File srcFile, File destFile)
  8. copyFile(File srcFile, File destFile)
  9. copyFile(File srcFile, File destFile)