Java FileOutputStream Write saveBytes(String filename, byte[] data)

Here you can find the source of saveBytes(String filename, byte[] data)

Description

Saves data to the file specified by filename.

License

Open Source License

Parameter

Parameter Description
filename a parameter
data a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void saveBytes(String filename, byte[] data) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) MOBAC developers/*from  w  w  w .j ava2s .  com*/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Saves <code>data</code> to the file specified by <code>filename</code>.
     * 
     * @param filename
     * @param data
     * @throws IOException
     */
    public static void saveBytes(String filename, byte[] data) throws IOException {
        FileOutputStream fo = null;
        try {
            fo = new FileOutputStream(filename);
            fo.write(data);
        } finally {
            closeStream(fo);
        }
    }

    public static void closeStream(InputStream in) {
        if (in == null)
            return;
        try {
            in.close();
        } catch (IOException e) {
        }
    }

    public static void closeStream(OutputStream out) {
        if (out == null)
            return;
        try {
            out.close();
        } catch (IOException e) {
        }
    }
}

Related

  1. saveBytes(byte[] bytes, File file)
  2. saveBytes(File f, byte[] content)
  3. saveBytes(File file, byte[] bytes)
  4. saveBytes(String filename, byte[] byteData)
  5. saveBytes(String filename, byte[] bytes)
  6. saveBytes(String filename, byte[] data)
  7. saveBytesToFile(byte[] bytes, File file)
  8. saveBytesToFile(byte[] bytes, File file)
  9. saveBytesToFile(byte[] data, File file)