Java BufferedWriter Write writeFile(File src, File dst)

Here you can find the source of writeFile(File src, File dst)

Description

write File

License

Open Source License

Declaration

public static void writeFile(File src, File dst) throws IOException 

Method Source Code

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

import java.io.*;

public class Main {
    public static void writeFile(File src, File dst) throws IOException {
        File parent = dst.getParentFile();
        if (!parent.exists())
            parent.mkdirs();//from  www .  j  a va2 s .  c  o m
        byte[] bytes = loadBytes(new FileInputStream(src));
        writeBytes(dst, bytes);
        if (!dst.exists())
            throw new RuntimeException("UNABLE TO WRITE FILE: "
                    + dst.getAbsolutePath());
    }

    public static void writeFile(File file, String contents)
            throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(contents);
        writer.flush();
        writer.close();
        writer = null;
    }

    public static byte[] loadBytes(InputStream input) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(input);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int c = bis.read();
        while (c != -1) {
            out.write(c);
            c = bis.read();
        }
        return out.toByteArray();
    }

    public static void writeBytes(File file, byte[] bytes)
            throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(file));
        bos.write(bytes);
        bos.flush();
        bos.close();
    }
}

Related

  1. writeFile(BufferedWriter fw, String line)
  2. writeFile(double[][] toWrite, String outName)
  3. writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)
  4. writeFile(Object msg)
  5. writeFile(String dir, String file, String content)
  6. writeFile(String directory, String content, String fileName, boolean deleteFileBeforeWriting)
  7. WriteFile(String file)