Here you can find the source of writeFileContent(String filepath, byte[] content)
Parameter | Description |
---|---|
filepath | the file path to write to. |
content | the content to be written into the given file. |
Parameter | Description |
---|---|
IOException | if some error occurs while writing to file. |
public static void writeFileContent(String filepath, byte[] content) throws IOException
//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(); } } } } }