Java FileOutputStream Write Byte Array writeBytes(byte[] data, File file)

Here you can find the source of writeBytes(byte[] data, File file)

Description

write Bytes

License

Apache License

Declaration

public static void writeBytes(byte[] data, File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w ww . ja v a  2 s.  c o  m*/
 * Copyright (c) 2013. Knowledge Media Institute - The Open University
 *
 * 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.*;

public class Main {
    public static void writeBytes(byte[] data, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        try {
            writeBytes(data, out);
        } finally {
            out.close();
        }
    }

    public static void writeBytes(byte[] data, OutputStream out) throws IOException {
        transfer(new ByteArrayInputStream(data), out);
    }

    /**
     * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>.
     *
     * @param in  The InputStream to read data from.
     * @param out The OutputStream to write data to.
     * @return The total number of bytes transfered.
     */
    public static final long transfer(InputStream in, OutputStream out) throws IOException {
        long totalBytes = 0;
        int bytesInBuf = 0;
        byte[] buf = new byte[4096];

        while ((bytesInBuf = in.read(buf)) != -1) {
            out.write(buf, 0, bytesInBuf);
            totalBytes += bytesInBuf;
        }

        return totalBytes;
    }

    /**
     * Writes all bytes from an <tt>InputStream</tt> to a file.
     *
     * @param in   The <tt>InputStream</tt> containing the data to write to the file.
     * @param file The file to write the data to.
     * @return The total number of bytes written.
     * @throws IOException If an I/O error occured while trying to write the data to the
     *                     file.
     */
    public static final long transfer(InputStream in, File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);

        try {
            return transfer(in, out);
        } finally {
            out.close();
        }
    }

    /**
     * Transfers all characters that can be read from <tt>in</tt> to <tt>out</tt>
     * .
     *
     * @param in  The Reader to read characters from.
     * @param out The Writer to write characters to.
     * @return The total number of characters transfered.
     */
    public static final long transfer(Reader in, Writer out) throws IOException {
        long totalChars = 0;
        int charsInBuf = 0;
        char[] buf = new char[4096];

        while ((charsInBuf = in.read(buf)) != -1) {
            out.write(buf, 0, charsInBuf);
            totalChars += charsInBuf;
        }

        return totalChars;
    }

    /**
     * Writes all characters from a <tt>Reader</tt> to a file using the default
     * character encoding.
     *
     * @param reader The <tt>Reader</tt> containing the data to write to the file.
     * @param file   The file to write the data to.
     * @return The total number of characters written.
     * @throws IOException If an I/O error occured while trying to write the data to the
     *                     file.
     * @see java.io.FileWriter
     */
    public static final long transfer(Reader reader, File file) throws IOException {
        FileWriter writer = new FileWriter(file);

        try {
            return transfer(reader, writer);
        } finally {
            writer.close();
        }
    }
}

Related

  1. writeBytes(byte[] bytes, String file)
  2. writeBytes(File aFile, byte theBytes[])
  3. writeBytes(File dest, byte[] data, int off, int len, boolean append)
  4. writeBytes(File f, byte[] b)
  5. writeBytes(File f, byte[] data)