Java Write String to File writeFile(String destination, byte[] data, boolean append)

Here you can find the source of writeFile(String destination, byte[] data, boolean append)

Description

writeFile

License

Open Source License

Parameter

Parameter Description
destination a parameter
data a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFile(String destination, byte[] data, boolean append) throws IOException 

Method Source Code


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

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**//from  w ww  .j  a v  a2 s .c om
     * writeFile
     * 
     * @param destination
     * @param data
     * @throws IOException
     */
    public static void writeFile(String destination, byte[] data, boolean append) throws IOException {

        File temp = new File(destination);

        if (temp.exists() && append == true) {
            FileOutputStream fos = new FileOutputStream(temp, true);
            try {
                fos.write(data);
                fos.flush();
            } finally {
                fos.close();
            }
        } else {

            FileOutputStream fos = new FileOutputStream(temp);
            try {
                fos.write(data);
                fos.flush();
            } finally {
                fos.close();
            }
        }
    }

    /**
     * existFile
     * 
     * @param path
     * @return
     */
    public static boolean exists(String path) {

        try {
            File temp = new File(path);
            return temp.exists();
        } catch (Exception e) {
            // ignore
        }
        return false;
    }
}

Related

  1. writeFile(String contents, String filename)
  2. writeFile(String contents, String fileName)
  3. writeFile(String data, File file)
  4. writeFile(String data, File tar)
  5. writeFile(String datafile, String filePath)
  6. writeFile(String filename, Object object)
  7. writeFile(String fileName, String content)
  8. writeFile(String filename, String content)
  9. writeFile(String filename, String content)