Here you can find the source of writeFile(byte[] fileData, String path)
Parameter | Description |
---|---|
fileData | Data to be written t a file. |
path | Path where the file should be written to. |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
public static void writeFile(byte[] fileData, String path) throws FileNotFoundException, IOException
//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) { ; } } } }