Java FileOutputStream Create writeFileContent(String filepath, byte[] content)

Here you can find the source of writeFileContent(String filepath, byte[] content)

Description

Write content into a file.

License

Open Source License

Parameter

Parameter Description
filepath the file path to write to.
content the content to be written into the given file.

Exception

Parameter Description
IOException if some error occurs while writing to file.

Declaration

public static void writeFileContent(String filepath, byte[] content) throws IOException 

Method Source Code

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

import java.io.BufferedOutputStream;

import java.io.File;

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

public class Main {
    /**//from w ww . j  a v a2  s . co m
     * Write content into a file.
     * 
     * @param filepath
     *          the file path to write to.
     * @param content
     *          the content to be written into the given file.
     * @throws IOException
     *           if some error occurs while writing to file.
     */
    public static void writeFileContent(String filepath, byte[] content) throws IOException {
        writeFileContent(new File(filepath), content);
    }

    /**
     * Write content into a file.
     * 
     * @param f
     *          the file to write to.
     * @param content
     *          the content to be written into the given file.
     * @throws IOException
     *           if some error occurs while writing to file.
     */
    public static void writeFileContent(File f, byte[] content) throws IOException {
        if (f == null) {
            throw new IllegalArgumentException("No file information found.");
        }
        if (content == null) {
            throw new IllegalArgumentException("The content to write should not be null.");
        }

        if (f != null && content != null) {
            if (!f.getParentFile().isDirectory()) {
                f.getParentFile().mkdirs();
            }
            BufferedOutputStream bufferedOutputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(f);
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                bufferedOutputStream.write(content);
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            } finally {
                if (bufferedOutputStream != null) {
                    try {
                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    } catch (Exception e) {
                    }
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
        }
    }
}

Related

  1. writeFileBinary(String filename, byte[]... dataArrays)
  2. writeFileByBytes(String content, String filename)
  3. writeFileByChar(String content, String filename, String encoding)
  4. writeFileByLine(String content, String filename, boolean append)
  5. writeFileBytes(File pFile, byte[] pBytes)
  6. writeFileContent(String sFileName, byte[] content)
  7. writeFileCover(String path, String content)
  8. writeFileData(File dataFile, String targetFile)
  9. writeFileData(File file, byte[] fileData)