Java FileInputStream Copy copyFile(File src, File dst)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File src, File dst) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w  w. j  a v a 2s  .c  o m
 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * 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
 *
 * Contributors:
 *     bstefanescu
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStreamWriter;

public class Main {
    public static void copyFile(File src, File dst) throws IOException {
        FileInputStream in = new FileInputStream(src);
        try {
            writeTo(in, dst);
        } finally {
            in.close();
        }
    }

    public static void writeTo(String data, File file) throws IOException {
        FileWriter w = new FileWriter(file);
        try {
            w.write(data);
        } finally {
            w.close();
        }
    }

    public static void writeTo(String data, File file, String charset) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out, charset));
        try {
            w.write(data);
        } finally {
            w.close();
        }
    }

    public static void writeTo(InputStream in, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        try {
            byte[] buf = new byte[1024 * 1024];
            int r = -1;
            while ((r = in.read(buf)) > -1) {
                if (r > 0) {
                    out.write(buf, 0, r);
                }
            }
        } catch (IOException e) {
            file.delete();
            throw e;
        } finally {
            out.close();
        }
    }

    public static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        int size = in.available();
        if (size < 1 || size > 64 * 1024) {
            size = 64 * 1024;
        }
        byte[] buffer = new byte[size];
        try {
            int read;
            while ((read = in.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, read));
            }
        } finally {
            in.close();
        }
        return sb.toString();
    }
}

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)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File dst)
  7. copyFile(File src, File dst)
  8. copyFile(File src, File dst)
  9. copyFile(File src, File dst)