Java Write Byte Array to File writeFile(byte[] fileData, String path)

Here you can find the source of writeFile(byte[] fileData, String path)

Description

Writes the given byte array to a file.

License

Apache License

Parameter

Parameter Description
fileData Data to be written t a file.
path Path where the file should be written to.

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void writeFile(byte[] fileData, String path) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//  w  ww.  jav  a  2 s.com
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.File;

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

public class Main {
    /**
     * Writes the given byte array to a file.
     * 
     * @param fileData
     *            Data to be written t a file.
     * @param path
     *            Path where the file should be written to.
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void writeFile(byte[] fileData, String path) throws FileNotFoundException, IOException {
        File fileToWrite = new File(path);
        if (!fileToWrite.exists()) {
            File pathToFile = new File(fileToWrite.getParent());
            pathToFile.mkdirs();
            fileToWrite.createNewFile();
        }
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(fileToWrite);
            outStream.write(fileData);
        } finally {
            try {
                if (outStream != null)
                    outStream.close();
            } catch (IOException e) {
                ;
            }
        }
    }
}

Related

  1. writeFile(byte[] data, File file)
  2. writeFile(byte[] data, File outFile)
  3. writeFile(byte[] data, String fileName)
  4. writeFile(byte[] data, String filePath)
  5. writeFile(byte[] data, String path, boolean overwrite)
  6. writeFile(byte[] outputBytes, String outputFile)
  7. writeFile(File f, byte[] data)
  8. writeFile(File file, byte[] b)
  9. writeFile(File file, byte[] content)