Java FileInputStream Copy copyFile(String quelle, String ziel)

Here you can find the source of copyFile(String quelle, String ziel)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String quelle, String ziel) 

Method Source Code

//package com.java2s;
/**//from  ww  w .j  a  v  a 2  s.c  o m
 * Title:        efa - elektronisches Fahrtenbuch f?r Ruderer
 * Copyright:    Copyright (c) 2001-2011 by Nicolas Michael
 * Website:      http://efa.nmichael.de/
 * License:      GNU General Public License v2
 *
 * @author Nicolas Michael
 * @version 2
 */

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

public class Main {
    public static boolean copyFile(String quelle, String ziel) {
        if ((new File(quelle)).equals(new File(ziel))) {
            return false; // Quelle und Ziel sind dieselbe Datei!
        }
        final int BUFSIZE = 4096;
        FileInputStream f1;
        FileOutputStream f2;
        byte[] buf = new byte[BUFSIZE];
        int n;
        if (!canOpenFile(quelle)) {
            return false;
        }
        try {
            f1 = new FileInputStream(quelle);
            f2 = new FileOutputStream(ziel);
            while ((n = f1.read(buf, 0, BUFSIZE)) > 0) {
                f2.write(buf, 0, n);
            }
            f1.close();
            f2.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static boolean canOpenFile(String d) {
        FileReader f;
        try {
            f = new FileReader(d);
            f.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

Related

  1. copyFile(String oldPath, String newPath)
  2. copyFile(String oldPathFile, String newPathFile)
  3. copyFile(String original, String copy)
  4. copyFile(String pathOld, String pathNew)
  5. copyFile(String pathOrig, String pathDst)
  6. copyFile(String resourceFimeName, String targetFileName)
  7. copyFile(String s, String s1)
  8. copyFile(String source, String dest)
  9. copyFile(String source, String destination)