Here you can find the source of writeBytes(final File file, final byte[] bytes)
Parameter | Description |
---|---|
file | The file to write the bytes to. |
bytes | The bytes to write. |
Parameter | Description |
---|---|
IOException | When writing to the file failed. |
public static void writeBytes(final File file, final byte[] bytes) throws IOException
//package com.java2s; /*/*w w w . j a v a 2 s. c o m*/ * Copyright (C) 2010 Klaus Reimer <k@ailis.de> * See LICENSE.md for licensing information. */ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Writes bytes to the specified file * * @param file * The file to write the bytes to. * @param bytes * The bytes to write. * @throws IOException * When writing to the file failed. */ public static void writeBytes(final File file, final byte[] bytes) throws IOException { try (final ByteArrayInputStream input = new ByteArrayInputStream(bytes); final FileOutputStream output = new FileOutputStream(file)) { copy(input, output); } } /** * Copies data from the input stream to the output stream. * * @param input * The input stream. * @param output * The output stream * @throws IOException * When copying failed. */ public static void copy(final InputStream input, final OutputStream output) throws IOException { final byte[] buffer = new byte[8192]; int read; while ((read = input.read(buffer)) > 0) { output.write(buffer, 0, read); } } }