Java FileReader Copy copyFileToDir(File inputFile, File outputDir)

Here you can find the source of copyFileToDir(File inputFile, File outputDir)

Description

copy File To Dir

License

Open Source License

Declaration

public static boolean copyFileToDir(File inputFile, File outputDir) 

Method Source Code

//package com.java2s;
/**//from w  w  w. j  ava  2  s  .co m
 * Visual Tracer - An Application of Java Code Instrumentation using AspectJ 
 * Copyright (C) 2010  
 * Carlos Correia - mail.cefc@gmail.com 
 * Rute Oliveira - rute23@gmail.com
 * Manuel Menezes de Sequeira - manuel.sequeira@iscte.pt
 * 
 * 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/>.
 */

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static boolean copyFileToDir(File inputFile, File outputDir) {
        try {
            String outputFileName = inputFile.getName();
            int index = 1;
            while (existFileInDir(outputFileName, outputDir)) {
                outputFileName = index + inputFile.getName();
                index++;
            }
            String directory = getDirectoryWithSlash(outputDir.getAbsolutePath());
            File outputFile = new File(directory + outputFileName);

            FileReader in = new FileReader(inputFile);
            FileWriter out = new FileWriter(outputFile);
            int c;

            while ((c = in.read()) != -1)
                out.write(c);

            in.close();
            out.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static boolean existFileInDir(String fileName, File dir) {
        String directory = getDirectoryWithSlash(dir.getAbsolutePath());
        return new File(directory + fileName).exists();
    }

    /**
     * Validate if this string have final slash
     * e.g. C:\folder\ or C:\folder/
     * @param directory
     * @return What?
     */
    public static String getDirectoryWithSlash(String directory) {
        if (directory.lastIndexOf("\\") != (directory.length() - 1)
                && directory.lastIndexOf("/") != (directory.length() - 1))
            directory += "\\";
        return directory;
    }
}

Related

  1. copyFile(String fromFilePath, String toFileDir)
  2. copyFile(String input, String output)
  3. copyFile(String inputName, String destination)
  4. copyFile(String sourceFileName, String targetFileName)
  5. copyFile(String srcFileName, String dstFileName)