Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

In this page you can find the example usage for java.io BufferedOutputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:me.daququ.common.core.utils.FileUtils.java

public static boolean byte2File(byte[] buff, String filePath, String fileName) throws Exception {
    boolean retFlag = true;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;//from www .j  a va2  s  .  c  om
    File file = null;
    try {
        File dir = new File(filePath);
        if (!(dir.exists() && dir.isDirectory())) {
            dir.mkdirs();
        }
        file = new File(filePath + fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(buff);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                throw new Exception(e.getMessage(), e);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                throw new Exception(e.getMessage(), e);
            }
        }
    }
    return retFlag;
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    File ret = null;//from ww  w .j a v  a  2 s  .c  om
    BufferedOutputStream stream = null;
    try {
        ret = new File(outputFile);
        if (!ret.exists()) {
            if (!ret.mkdirs()) {
                throw new FileNotFoundException("can't create folder" + outputFile);
            }

        }
        FileOutputStream fstream = new FileOutputStream(ret);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        // log.error("helper:get file from byte process error!");
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // log.error("helper:get file from byte process error!");
                e.printStackTrace();
            }
        }
    }
    return ret;
}

From source file:JarMaker.java

/**
 * @param f_name : source zip file path name
 * @param dir_name : target dir file path
 *//*  w w w  .j av  a 2s  .  co  m*/
public static void unpackJar(String f_name, String dir_name) throws IOException {

    BufferedInputStream bism = new BufferedInputStream(new FileInputStream(f_name));

    ZipInputStream zism = new ZipInputStream(bism);

    for (ZipEntry z = zism.getNextEntry(); z != null; z = zism.getNextEntry()) {
        File f = new File(dir_name, z.getName());
        if (z.isDirectory()) {
            f.mkdirs();
        } else {
            f.getParentFile().mkdirs();

            BufferedOutputStream bosm = new BufferedOutputStream(new FileOutputStream(f));
            int i;
            byte[] buffer = new byte[1024 * 512];
            try {
                while ((i = zism.read(buffer, 0, buffer.length)) != -1)
                    bosm.write(buffer, 0, i);
            } catch (IOException ie) {
                throw ie;
            }
            bosm.close();
        }
    }
    zism.close();
    bism.close();
}

From source file:com.dc.util.file.FileSupport.java

public static void writeBinaryFile(InputStream in, File targetFile, boolean encrypted, String key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {//from   w w w  .  j  av  a  2  s.  c o  m
        bis = new BufferedInputStream(in);
        bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        if (encrypted) {
            copyEncryptedStream(bis, bos, key);
        } else {
            copyStream(bis, bos);
        }
    } finally {
        if (bis != null) {
            bis.close();
        }
        if (bos != null) {
            bos.close();
        }
    }

}

From source file:it.geosolutions.tools.compress.file.reader.TarReader.java

private static void writeFile(File curr_dest, TarArchiveInputStream tis) throws IOException {
    byte[] buf = new byte[Conf.getBufferSize()];
    FileOutputStream fos = null;//w ww .ja v a  2 s  .  com
    BufferedOutputStream bos_inner = null;
    try {
        fos = new FileOutputStream(curr_dest);
        bos_inner = new BufferedOutputStream(fos, Conf.getBufferSize());
        int read_sz = 0;
        while ((read_sz = tis.read(buf)) != -1) {
            bos_inner.write(buf, 0, read_sz);
            bos_inner.flush();
        }
        //        } catch (IOException e){
    } finally {
        if (bos_inner != null) {
            try {
                bos_inner.close();
            } catch (IOException e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:de.tu_dortmund.ub.data.util.TPUUtil.java

public static String writeResultToFile(final CloseableHttpResponse httpResponse, final Properties config,
        final String exportDataModelID, final String fileEnding) throws IOException, TPUException {

    LOG.info("try to write result to file");

    final String persistInFolderString = config.getProperty(TPUStatics.PERSIST_IN_FOLDER_IDENTIFIER);
    final boolean persistInFolder = Boolean.parseBoolean(persistInFolderString);
    final HttpEntity entity = httpResponse.getEntity();

    final String fileName;

    if (persistInFolder) {

        final InputStream responseStream = entity.getContent();
        final BufferedInputStream bis = new BufferedInputStream(responseStream, 1024);

        final String resultsFolder = config.getProperty(TPUStatics.RESULTS_FOLDER_IDENTIFIER);
        fileName = resultsFolder + File.separatorChar + EXPORT_FILE_NAME_PREFIX + exportDataModelID + DOT
                + fileEnding;/*from w  w w .  java  2s  .  c  o  m*/

        LOG.info(String.format("start writing result to file '%s'", fileName));

        final FileOutputStream outputStream = new FileOutputStream(fileName);
        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        IOUtils.copy(bis, bufferedOutputStream);
        bufferedOutputStream.flush();
        outputStream.flush();
        bis.close();
        responseStream.close();
        bufferedOutputStream.close();
        outputStream.close();

        checkResultForError(fileName);
    } else {

        fileName = "[no file name available]";
    }

    EntityUtils.consume(entity);

    return fileName;
}

From source file:com.elastica.helper.FileUtility.java

public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException {
    File firefoxProfile = new File(storeLocation);
    String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile();

    JarFile jar = new JarFile(location);
    System.out.println("Extracting jar file::: " + location);
    firefoxProfile.mkdir();// ww w  . ja v  a  2 s.c  om

    Enumeration<?> jarFiles = jar.entries();
    while (jarFiles.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) jarFiles.nextElement();
        String currentEntry = entry.getName();
        File destinationFile = new File(storeLocation, currentEntry);
        File destinationParent = destinationFile.getParentFile();

        // create the parent directory structure if required
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte[] data = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destinationFile);
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);

            // read and write till last byte
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                destination.write(data, 0, currentByte);
            }

            destination.flush();
            destination.close();
            is.close();
        }
    }

    FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF"));
    if (OSUtility.isWindows()) {
        new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete();
    } else {
        new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete();
    }
}

From source file:com.dc.util.file.FileSupport.java

public static void writeBinaryFile(File file, byte[] data) throws IOException {
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {//www. jav  a  2 s  .  c o m
        fileOutputStream = new FileOutputStream(file);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write(data, 0, data.length);
        bufferedOutputStream.flush();
    } finally {
        if (bufferedOutputStream != null) {
            fileOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
    }
}

From source file:Main.java

public static void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException {
    BufferedOutputStream os = null;
    try {/*from w w w. j av  a  2 s  . c om*/
        File file = new File(_file);
        // String _filePath_file.replace(File.separatorChar +
        // file.getName(), "");
        int end = _file.lastIndexOf(File.separator);
        String _filePath = _file.substring(0, end);
        File filePath = new File(_filePath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        file.createNewFile();
        os = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e("TAG_ERROR", e.getMessage(), e);
            }
        }
    }
}

From source file:Main.java

public static boolean copyFile(File fromFile, File toFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/*from   w  w  w.j a  v a  2s. c  om*/
        FileInputStream is = new FileInputStream(fromFile);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(toFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}