Java BufferedReader Copy copyFileRecursivlyLinewise(File inDir, File outDir)

Here you can find the source of copyFileRecursivlyLinewise(File inDir, File outDir)

Description

Copies the directory structure with all the included files to a new directory.

License

Open Source License

Parameter

Parameter Description
inDir directory that shall be copied
outDir directory the files and directories shall be copied to

Exception

Parameter Description
IOException thrown if reading or writing has failed

Declaration

public static void copyFileRecursivlyLinewise(File inDir, File outDir) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    /**//from ww  w .  j  a va  2  s .c o  m
     * Copies the directory structure with all the included files to a new directory.
     * The files are processed linewise, i.e., the OS specific newline character will be used
     * to break lines.
     *
     * @param inDir directory that shall be copied
     * @param outDir directory the files and directories shall be copied to
     * @throws IOException thrown if reading or writing has failed
     */
    public static void copyFileRecursivlyLinewise(File inDir, File outDir) throws IOException {
        File[] inputFiles = inDir.listFiles();
        for (File inputFile : inputFiles) {
            File outputFile = new File(outDir, inputFile.getName());
            // copy program files line-wise to convert use OS specific newlines
            if (inputFile.isFile()) {
                copyFileLinewise(inputFile, outputFile);
            } else {
                File newOutDir = new File(outDir, inputFile.getName());
                newOutDir.mkdir();
                copyFileRecursivlyLinewise(inputFile, newOutDir);
            }
        }
    }

    /**
     * Copies one file linewise to another file.
     *
     * @param inputFile input file (that shall be copied)
     * @param outputFile destination file
     * @throws IOException thrown if reading or writing has failed
     */
    public static void copyFileLinewise(File inputFile, File outputFile) throws IOException {
        try {
            BufferedReader r = new BufferedReader(new FileReader(inputFile));
            BufferedWriter w = new BufferedWriter(new FileWriter(outputFile));

            String line;
            while ((line = r.readLine()) != null) {
                w.write(line);
                w.newLine();
            }

            w.close();
            r.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(String srcFileName, String destFileName)
  2. copyFile2(String source, String out)
  3. copyFileContentsIntoAnotherFile(String inputFileName, PrintWriter Out)
  4. copyFileFromJar(File jarFile, File targetFile, String sourceFilePath)
  5. copyFileLinewise(File inputFile, File outputFile)
  6. copyFileTextMode(String source, String destination)
  7. copyFileWithAppend(File source, File destination)