Java BufferedWriter Write writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)

Here you can find the source of writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)

Description

Writes fileRef to dir as newFileRef.

License

Open Source License

Parameter

Parameter Description
dir The directory to write to
fileRef The original filename (path)
newFileName The new filename
classLoader The class loader that contains <code>fileRef</code>

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)
        throws IOException 

Method Source Code


//package com.java2s;
/*/* w ww  .  ja va  2 s . com*/
Toolforge core - Core of the Toolforge application suite
Copyright (C) 2004  Toolforge <www.toolforge.nl>
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library 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
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**
     * Writes <code>fileRef</code> to <code>dir</code> as <code>newFileRef</code>. <code>fileRef</code> should be available in the classpath of
     * <code>classLoader</code>.
     *
     * @param dir         The directory to write to
     * @param fileRef     The original filename (path)
     * @param newFileName The new filename
     * @param classLoader The class loader that contains <code>fileRef</code>
     *
     * @throws IOException
     */
    public static void writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)
            throws IOException {

        if (dir == null || fileRef == null || newFileName == null || "".equals(newFileName)
                || classLoader == null) {
            throw new NullPointerException("");
        }

        BufferedReader in = new BufferedReader(
                new InputStreamReader(classLoader.getResourceAsStream(fileRef.getPath())));

        dir.mkdirs();

        if (newFileName.getPath().lastIndexOf("/") > 0) {
            String subDir = newFileName.getPath().substring(0, newFileName.getPath().lastIndexOf("/"));
            new File(dir, subDir).mkdirs();
        }

        BufferedWriter out = new BufferedWriter(new FileWriter(new File(dir, newFileName.getPath())));

        String str;
        while ((str = in.readLine()) != null) {
            out.write(str);
        }
        out.close();
        in.close();
    }
}

Related

  1. writeFile(BufferedWriter fw, String line)
  2. writeFile(double[][] toWrite, String outName)
  3. writeFile(File src, File dst)
  4. writeFile(Object msg)
  5. writeFile(String dir, String file, String content)
  6. writeFile(String directory, String content, String fileName, boolean deleteFileBeforeWriting)