Java FileOutputStream Write save(String filename, byte[] bytes)

Here you can find the source of save(String filename, byte[] bytes)

Description

save

License

Apache License

Declaration

public static void save(String filename, byte[] bytes) 

Method Source Code


//package com.java2s;
/*/*from w ww  .  j  a 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.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    public static final int BUFFER_SIZE = 65536;

    public static void save(String filename, byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            save(filename, bais);
            bais.close();
            bais = null;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException ex) {
            }
        }
    }

    public static void save(String filename, InputStream inputStream) {
        if (filename == null || inputStream == null) {
            return;
        }
        String path = "";
        String sp = System.getProperty("file.separator");
        if (filename.indexOf(sp) != -1) {
            path = filename.substring(0, filename.lastIndexOf(sp));
        }
        if (filename.indexOf("/") != -1) {
            path = filename.substring(0, filename.lastIndexOf("/"));
        }
        path = getJavaFileSystemPath(path);
        java.io.File dir = new java.io.File(path + sp);
        mkdirsWithExistsCheck(dir);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(new FileOutputStream(filename));
            int bytesRead = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = bis.read(buffer, 0, BUFFER_SIZE)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.flush();
            bis.close();
            bos.close();
            bis = null;
            bos = null;
        } catch (IOException ex) {
            bis = null;
            bos = null;
            throw new RuntimeException(ex);
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                    bis = null;
                }
                if (bos != null) {
                    bos.close();
                    bos = null;
                }
            } catch (IOException ioe) {
            }
        }
    }

    public static String getJavaFileSystemPath(String path) {
        if (path == null) {
            return null;
        }
        path = path.replace('\\', '/');
        return path;
    }

    public static boolean mkdirsWithExistsCheck(File dir) {
        if (dir.mkdir() || dir.exists()) {
            return true;
        }
        File canonDir = null;
        try {
            canonDir = dir.getCanonicalFile();
        } catch (IOException e) {
            return false;
        }
        String parent = canonDir.getParent();
        return (parent != null)
                && (mkdirsWithExistsCheck(new File(parent)) && (canonDir.mkdir() || canonDir.exists()));
    }
}

Related

  1. save(final InputStream in, final File file)
  2. save(InputStream in, File f)
  3. save(String content, String fileName)
  4. save(String data, String encoding, String file)
  5. save(String f, byte[] data)
  6. save(String path, byte[] content, IProgressMonitor monitor)
  7. saveArray(byte[] pictureBytes, OutputStream stream)
  8. saveBase64Str2Disk(String base64Str, File file)
  9. saveBase64strToFile(String base64Str, String filePath)