Java FileInputStream Copy copyFile(String src, String tar)

Here you can find the source of copyFile(String src, String tar)

Description

copyFile

License

Open Source License

Parameter

Parameter Description
src a parameter
tar a parameter

Declaration

public static int copyFile(String src, String tar) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from   ww  w  .j av  a  2 s.  c  o  m
     * copyFile
     * 
     * @param src
     * @param tar
     * @return
     */
    public static int copyFile(String src, String tar) {

        int res = 0;

        File srcFile = new File(src);

        if (srcFile.exists()) {

            try {
                byte[] data = readFile(src);

                writeFile(tar, data, false);

                res = 1;
            } catch (Exception ee) {
                // ignore
            }
        }

        return res;
    }

    /**
     * existFile
     * 
     * @param path
     * @return
     */
    public static boolean exists(String path) {

        try {
            File temp = new File(path);
            return temp.exists();
        } catch (Exception e) {
            // ignore
        }
        return false;
    }

    /**
     * readFile
     * 
     * @param destination
     * @return
     * @throws IOException
     */
    public static byte[] readFile(String destination) throws IOException {

        File temp = new File(destination);

        if (temp.exists()) {
            FileInputStream fis = new FileInputStream(temp);
            byte[] data = new byte[(int) temp.length()];
            try {
                fis.read(data);
            } finally {
                fis.close();
            }
            return data;
        }

        return new byte[0];
    }

    /**
     * writeFile
     * 
     * @param destination
     * @param data
     * @throws IOException
     */
    public static void writeFile(String destination, byte[] data, boolean append) throws IOException {

        File temp = new File(destination);

        if (temp.exists() && append == true) {
            FileOutputStream fos = new FileOutputStream(temp, true);
            try {
                fos.write(data);
                fos.flush();
            } finally {
                fos.close();
            }
        } else {

            FileOutputStream fos = new FileOutputStream(temp);
            try {
                fos.write(data);
                fos.flush();
            } finally {
                fos.close();
            }
        }
    }
}

Related

  1. copyFile(String src, String dest)
  2. copyFile(String src, String dest)
  3. copyFile(String src, String dest)
  4. copyFile(String src, String dst)
  5. copyFile(String src, String dst)
  6. copyFile(String src, String to)
  7. copyFile(String srcDir, String destDir)
  8. copyFile(String srcFile, String destFile)
  9. copyFile(String srcFile, String dstFile)