Java FileOutputStream Create writeFile(String file, byte[]... data)

Here you can find the source of writeFile(String file, byte[]... data)

Description

write File

License

Open Source License

Declaration

public static void writeFile(String file, byte[]... data) 

Method Source Code

//package com.java2s;
/*//from   w ww .  j a v a  2  s . c o  m
 *  Copyright (C) 2010-2016 JPEXS, All rights reserved.
 *
 * 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 3.0 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.
 */

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

import java.io.OutputStream;

public class Main {
    public static void writeFile(String file, byte[]... data) {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            for (byte[] d : data) {
                fos.write(d);
            }
        } catch (IOException ex) {
            // ignore
        }
    }

    public static void writeFile(String file, InputStream stream) {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            copyStream(stream, fos);
        } catch (IOException ex) {
            // ignore
        }
    }

    public static void copyStream(InputStream is, OutputStream os) {
        try {
            final int bufSize = 4096;
            byte[] buf = new byte[bufSize];
            int cnt = 0;
            while ((cnt = is.read(buf)) > 0) {
                os.write(buf, 0, cnt);
            }
        } catch (IOException ex) {
            // ignore
        }
    }

    public static void copyStream(InputStream is, OutputStream os,
            long maxLength) {
        try {
            final int bufSize = 4096;
            byte[] buf = new byte[bufSize];
            int cnt = 0;
            while ((cnt = is.read(buf)) > 0) {
                os.write(buf, 0, cnt);
                maxLength -= cnt;

                // last chunk is smaller
                if (maxLength < bufSize) {
                    buf = new byte[(int) maxLength];
                }
            }
        } catch (IOException ex) {
            // ignore
        }
    }
}

Related

  1. writeFile(InputStream stream, File to)
  2. writeFile(List data, String filename)
  3. writeFile(String directoryPath, String filename, byte[] bytes)
  4. writeFile(String f, String s)
  5. writeFile(String file, byte[]... data)
  6. writeFile(String fileName)
  7. writeFile(String fileOutput, byte[] data)
  8. writeFile(String filePath, byte[] bytes)
  9. writeFile(String filePath, Object obj)