Java FileOutputStream Write Byte Array writeBytes(File filename, byte[] contents)

Here you can find the source of writeBytes(File filename, byte[] contents)

Description

Write out a file to the File specified.

License

Open Source License

Parameter

Parameter Description
filename File to write to
contents file contents

Exception

Parameter Description
FileNotFoundException if the file does not exist
IOExceptionif there is a problem writing

Declaration

public static void writeBytes(File filename, byte[] contents) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j av  a2  s. co m*/
 * Copyright 1997-2016 Unidata Program Center/University Corporation for Atmospheric Research
 * Copyright 2010-2015 Jeff McWhirter
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    /**
     * Write out a file to the {@link File} specified.
     *
     * @param filename  File to write to
     * @param contents  file contents
     *
     * @throws FileNotFoundException    if the file does not exist
     * @throws IOException              if there is a problem writing
     */
    public static void writeBytes(File filename, byte[] contents) throws FileNotFoundException, IOException {
        FileOutputStream out = new FileOutputStream(filename);
        out.write(contents);
        out.flush();
        out.close();
    }

    /**
     * Write the bytes held by the iven string to the outputstream
     *
     * @param to stream to write to
     * @param s string to write
     *
     * @throws Exception on badness
     */
    public static void write(OutputStream to, String s) throws Exception {
        to.write(s.getBytes());
    }

    /**
     * _more_
     *
     * @param inputStream _more_
     */
    public static void close(InputStream inputStream) {
        if (inputStream == null) {
            return;
        }
        try {
            inputStream.close();
        } catch (Exception ignore) {
        }
    }

    /**
     * _more_
     *
     * @param outputStream _more_
     */
    public static void close(OutputStream outputStream) {
        if (outputStream == null) {
            return;
        }
        try {
            outputStream.close();
        } catch (Exception ignore) {
        }
    }
}

Related

  1. writeBytes(File file, byte[] bytes)
  2. writeBytes(File file, byte[] bytes, boolean append)
  3. writeBytes(File file, byte[] data)
  4. writeBytes(File file, byte[] pattern, int repeat)
  5. writeBytes(File file, byte[] source, int offset, int len)
  6. writeBytes(File outputFile, byte[] bytes)
  7. writeBytes(final File file, final byte[] bytes)
  8. writeBytes(final File file, final byte[] bytes)
  9. writeBytes(int[] data, String filename)