Java FileInputStream Copy copyFile(String pathOrig, String pathDst)

Here you can find the source of copyFile(String pathOrig, String pathDst)

Description

Copia de ficheros

License

Open Source License

Parameter

Parameter Description
pathOrig Ruta de origen
pathDst Ruta de destino.

Declaration

public static void copyFile(String pathOrig, String pathDst) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
 *
 * Copyright (C) 2008 IVER T.I. and Generalitat Valenciana.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 *//*from   ww w  . ja  v a2  s  .com*/

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

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

public class Main {
    /**
     * Copia de ficheros
     * @param pathOrig Ruta de origen
     * @param pathDst Ruta de destino.
     */
    public static void copyFile(String pathOrig, String pathDst) throws FileNotFoundException, IOException {
        InputStream in;
        OutputStream out;

        if (pathOrig == null || pathDst == null) {
            System.err.println("Error en path");
            return;
        }

        File orig = new File(pathOrig);
        if (!orig.exists() || !orig.isFile() || !orig.canRead()) {
            System.err.println("Error en fichero de origen");
            return;
        }

        File dest = new File(pathDst);
        String file = new File(pathOrig).getName();
        if (dest.isDirectory())
            pathDst += file;

        in = new FileInputStream(pathOrig);
        out = new FileOutputStream(pathDst);

        byte[] buf = new byte[1024];
        int len;

        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);

        in.close();
        out.close();
    }
}

Related

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