Java Text File Write writeStringToFile(String fullPath, byte[] fileBytes)

Here you can find the source of writeStringToFile(String fullPath, byte[] fileBytes)

Description

write String To File

License

Apache License

Declaration

public static boolean writeStringToFile(String fullPath, byte[] fileBytes) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedOutputStream;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.Writer;

public class Main {
    public static boolean writeStringToFile(String fullPath, String content) {
        try {//from  w ww  . j  ava  2s  .  com
            Writer output = null;
            File file = new File(fullPath);
            if (!file.exists()) {
                file.createNewFile();
            }
            output = new BufferedWriter(new FileWriter(file));
            output.write(content);
            output.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean writeStringToFile(String fullPath, byte[] fileBytes) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fullPath));
            bos.write(fileBytes);
            bos.flush();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * Closes InputStream and/or OutputStream. It makes sure that both streams
     * tried to be closed, even first throws an exception.
     * 
     * @throw IOException if stream (is not null and) cannot be closed.
     * 
     */
    protected static void close(InputStream iStream, OutputStream oStream) throws IOException {
        try {
            if (iStream != null) {
                iStream.close();
            }
        } finally {
            if (oStream != null) {
                oStream.close();
            }
        }
    }
}

Related

  1. writeStringToFile(String filename, String string)
  2. writeStringToFile(String filename, String string, boolean overwrite)
  3. writeStringToFile(String filename, String stringToWrite)
  4. writeStringToFile(String filename, String text)
  5. WriteStringToFile(String fn, String dest)
  6. writeStringToFile(String fullPath, String content)
  7. writeStringToFile(String jsonStr, File file)
  8. writeStringToFile(String macroContent, File file)
  9. writeStringToFile(String modifiedLine, String filePath)