Java BufferedReader Copy copyFile(String srcFile, String dstFile)

Here you can find the source of copyFile(String srcFile, String dstFile)

Description

Copy a file.

License

Open Source License

Parameter

Parameter Description
srcFile The source file.
dstFile The copied file.

Declaration

public static void copyFile(String srcFile, String dstFile) 

Method Source Code

//package com.java2s;
/*===============================================================================
 * Copyright (c) 2010-2012 University of Massachusetts.  All Rights Reserved.
 *
 * Use of the RankLib package is subject to the terms of the software license set 
 * forth in the LICENSE file included with this software, and also available at
 * http://people.cs.umass.edu/~vdang/ranklib_license.html
 *===============================================================================
 *//*  ww  w .j ava  2s .co  m*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
    /**
     * Copy a file.
     * @param srcFile The source file.
     * @param dstFile The copied file.
     */
    public static void copyFile(String srcFile, String dstFile) {
        try {
            FileInputStream fis = new FileInputStream(new File(srcFile));
            FileOutputStream fos = new FileOutputStream(new File(dstFile));
            try {
                byte[] buf = new byte[40960];
                int i = 0;
                while ((i = fis.read(buf)) != -1) {
                    fos.write(buf, 0, i);
                }
            } catch (Exception e) {
                System.out.println("Error in FileUtils.copyFile: " + e.toString());
            } finally {
                if (fis != null)
                    fis.close();
                if (fos != null)
                    fos.close();
            }
        } catch (Exception ex) {
            System.out.println("Error in FileUtils.copyFile: " + ex.toString());
        }
    }

    /**
     * Read the content of a file.
     * @param filename The file to read.
     * @param encoding The encoding of the file.
     * @return The content of the input file.
     */
    public static String read(String filename, String encoding) {
        BufferedReader in;
        String content = "";
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
            char[] newContent = new char[40960];
            int numRead = -1;
            while ((numRead = in.read(newContent)) != -1) {
                content += new String(newContent, 0, numRead);
            }
            in.close();
        } catch (Exception e) {
            content = "";
        }
        return content;
    }

    /**
     * Write a text to a file.
     * @param filename The output filename.
     * @param encoding The encoding of the file.
     * @param strToWrite The string to write.
     * @return TRUE if the procedure succeeds; FALSE otherwise.
     */
    public static boolean write(String filename, String encoding, String strToWrite) {
        BufferedWriter out = null;
        try {

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), encoding));
            out.write(strToWrite);
            out.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(String orig, String dest)
  3. copyFile(String source, String dest)
  4. copyFile(String src, String dst)
  5. copyFile(String src, String dst)
  6. copyFile(String srcFileName, String destFileName)
  7. copyFile2(String source, String out)
  8. copyFileContentsIntoAnotherFile(String inputFileName, PrintWriter Out)
  9. copyFileFromJar(File jarFile, File targetFile, String sourceFilePath)