Java FileInputStream Copy copyFile(String srFile, String dtFile)

Here you can find the source of copyFile(String srFile, String dtFile)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String srFile, String dtFile) 

Method Source Code

//package com.java2s;
/*/*from  w ww .j  av  a  2 s  . c  o  m*/
 * This file is part of Giswater
 * Copyright (C) 2013 Tecnics Associats
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *   David Erill <derill@giswater.org>
 */

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;
import java.io.PrintWriter;

import java.io.StringWriter;

import java.util.ResourceBundle;

import java.util.logging.Logger;

public class Main {
    private static final int NUM_LEVELS = 10;
    private static Logger logger;
    private static ResourceBundle bundleText;

    public static boolean copyFile(String srFile, String dtFile) {

        boolean ok = false;

        try {

            File f1 = new File(srFile);
            File f2 = new File(dtFile);
            InputStream in = new FileInputStream(f1);

            // For Overwrite the file.
            OutputStream out = new FileOutputStream(f2);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            logger.info("File from: " + srFile + "\nFile to:   " + dtFile);
            ok = true;

        } catch (FileNotFoundException e) {
            logError(e.getMessage() + " in the specified directory.");
        } catch (IOException e) {
            logError(e.getMessage());
        }

        return ok;

    }

    public static void logError(String msg) {
        logError(msg, "");
    }

    public static void logError(Exception e) {
        logError(e, "");
    }

    public static void logError(String msg, String param) {
        if (logger != null) {
            String errorMsg = getBundleString(msg);
            if (!param.equals("")) {
                errorMsg += "\nParameter: " + param;
            }
            logger.warning(errorMsg);
        }
    }

    public static void logError(Exception e, String param) {

        if (logger != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw, true);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            String fullStack = sw.toString();
            String[] lines = fullStack.split(System.getProperty("line.separator"));
            String shortStack = "";
            for (int i = 0; i < NUM_LEVELS; i++) {
                shortStack += lines[i] + "\n";
            }
            shortStack += "Parameter: " + param;
            logger.warning(shortStack);
        }

    }

    public static String getBundleString(String key) {
        return getBundleString(bundleText, key);
    }

    public static String getBundleString(ResourceBundle bundle, String key) {
        try {
            return bundle.getString(key);
        } catch (Exception e) {
            return key;
        }
    }
}

Related

  1. copyFile(String srcFilePath, String destFilePath)
  2. copyFile(String srcPath, String dstPath)
  3. copyFile(String srcPath, String dstPath, boolean replace)
  4. copyFile(String srFile, String dtFile)
  5. copyFile(String srFile, String dtFile)
  6. copyFile(String srFilePath, String dtFilePath)
  7. copyFile(String target, String source)
  8. copyFileAndReName(String destPath, String srcPath, String srcFile, String destFile)
  9. copyFileAsStream(String srcFilePathName, String dstFilePathName)