Java FileOutputStream Write Byte Array writeBytes(File file, byte[] pattern, int repeat)

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

Description

write Bytes

License

Apache License

Declaration

public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException 

Method Source Code


//package com.java2s;
/*/* w ww  .ja v  a 2 s  .com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.BufferedOutputStream;
import java.io.File;

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

public class Main {
    public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
        file.deleteOnExit();
        file.getParentFile().mkdirs();
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            for (int i = 0; i < repeat; i++) {
                out.write(pattern);
            }
            out.close();
            out = null;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (final IOException e) {
                // Suppressed due to an exception already thrown in the try block.
            }
        }
    }

    public static boolean mkdirs(File directory) {
        if (directory == null) {
            return false;
        }

        if (directory.exists()) {
            return false;
        }
        if (directory.mkdir()) {
            return true;
        }

        File canonDir = null;
        try {
            canonDir = directory.getCanonicalFile();
        } catch (IOException e) {
            return false;
        }

        File parentDir = canonDir.getParentFile();
        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
    }
}

Related

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