Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Util.java

/**
 * Writes the specified byte[] to the specified File path.
 * //from  w ww . jav  a 2  s.c  o m
 * @param theFile File Object representing the path to write to.
 * @param bytes The byte[] of data to write to the File.
 * @throws IOException Thrown if there is problem creating or writing the 
 * File.
 */
public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException {
    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream(theFile);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
    } finally {
        if (bos != null) {
            try {
                //flush and close the BufferedOutputStream
                bos.flush();
                bos.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.jcalvopinam.core.Unzipping.java

private static void joinAndUnzipFile(File inputFile, CustomFile customFile) throws IOException {

    ZipInputStream is = null;//from  w  w  w. j av  a2s  . com
    File path = inputFile.getParentFile();

    List<InputStream> fileInputStream = new ArrayList<>();
    for (String fileName : path.list()) {
        if (fileName.startsWith(customFile.getFileName()) && fileName.endsWith(Extensions.ZIP.getExtension())) {
            fileInputStream.add(new FileInputStream(String.format("%s%s", customFile.getPath(), fileName)));
            System.out.println(String.format("File found: %s", fileName));
        }
    }

    if (fileInputStream.size() > 0) {
        String fileNameOutput = String.format("%s%s", customFile.getPath(), customFile.getFileName());
        OutputStream os = new BufferedOutputStream(new FileOutputStream(fileNameOutput));
        try {
            System.out.println("Please wait while the files are joined: ");

            ZipEntry ze;
            for (InputStream inputStream : fileInputStream) {
                is = new ZipInputStream(inputStream);
                ze = is.getNextEntry();
                customFile.setFileName(String.format("%s_%s", REBUILT, ze.getName()));

                byte[] buffer = new byte[CustomFile.BYTE_SIZE];

                for (int readBytes; (readBytes = is.read(buffer, 0, CustomFile.BYTE_SIZE)) > -1;) {
                    os.write(buffer, 0, readBytes);
                    System.out.print(".");
                }
            }
        } finally {
            os.flush();
            os.close();
            is.close();
            renameFinalFile(customFile, fileNameOutput);
        }
    } else {
        throw new FileNotFoundException("Error: The file not exist!");
    }
    System.out.println("\nEnded process!");
}

From source file:net.rptools.maptool.client.AppSetup.java

/**
 * Overwrites any existing README file in the ~/.maptool/resource directory with the one from the current MapTool
 * JAR file. This way any updates to the README will eventually be seen by the user, although only when a new
 * directory is added to the resource library...
 * //w w w. jav a 2 s . co  m
 * @throws IOException
 */
private static void createREADME() throws IOException {
    File outFilename = new File(AppConstants.UNZIP_DIR, "README");
    InputStream inStream = null;
    OutputStream outStream = null;
    try {
        inStream = AppSetup.class.getResourceAsStream("README");
        outStream = new BufferedOutputStream(new FileOutputStream(outFilename));
        IOUtils.copy(inStream, outStream);
    } finally {
        IOUtils.closeQuietly(inStream);
        IOUtils.closeQuietly(outStream);
    }
}

From source file:br.com.tiagods.util.FTPDownload.java

public boolean downloadFile(String arquivo) {
    FTPConfig cf = FTPConfig.getInstance();
    FTPClient ftp = new FTPClient();
    try {//ww w  . j  av a 2  s  .  co m
        ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port")));
        ftp.login(cf.getValue("user"), cf.getValue("pass"));
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        String remoteFile1 = cf.getValue("dirFTP") + "/" + arquivo;
        novoArquivo = new File(System.getProperty("java.io.tmpdir") + "/" + arquivo);
        try (OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(novoArquivo))) {
            boolean success = ftp.retrieveFile(remoteFile1, outputStream1);
            if (success) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        System.out.println("Erro = " + e.getMessage());
        return false;
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.t3.net.LocalLocation.java

@Override
public void putContent(InputStream content) throws IOException {
    try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(getFile()))) {
        IOUtils.copy(content, out);/*from w  w  w .  j  a v a  2  s.  c  om*/
        IOUtils.closeQuietly(content);
    }
}

From source file:com.qasp.diego.arsp.Atualiza.java

public static boolean DownloadFTP() throws IOException {

    final String FTPURL = "37.187.45.24";
    final String USUARIO = "diego";
    final String SENHA = "Jogador5";
    final String ARQUIVO = "data.dat";
    FTPClient ftp = new FTPClient();

    try {/*from   ww  w  . ja  v  a2 s.  c o m*/
        ftp.connect(FTPURL);
        ftp.login(USUARIO, SENHA);
        ftp.changeWorkingDirectory("/httpdocs/tcc/TCCpython");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        OutputStream outputStream = null;
        boolean downloadcomsucesso = false;
        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    ARQUIVO);
            f.createNewFile();
            outputStream = new BufferedOutputStream(new FileOutputStream(f));
            downloadcomsucesso = ftp.retrieveFile(ARQUIVO, outputStream);
        } finally {
            if (outputStream != null)
                outputStream.close();
        }
        return downloadcomsucesso;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
}

From source file:edu.dfci.cccb.mev.limma.domain.simple.FileBackedLimma.java

@SneakyThrows
public static FileBackedLimma from(InputStream results) {
    FileBackedLimma result = new FileBackedLimma(new TemporaryFolder());
    try (OutputStream full = new BufferedOutputStream(new FileOutputStream(result.full));
            BufferedInputStream in = new BufferedInputStream(results)) {
        IOUtils.copy(in, full);/*www.  j  a  v a 2s.  c o  m*/
    }
    return result;
}

From source file:Main.java

/**
 * Method write./*from  w  w  w .  j a  v a 2  s.co  m*/
 * 
 * @param filepath
 *            String
 * @param theProperties
 *            Properties
 * @param propComments
 *            String
 * @throws IOException
 */
public static void write(String filepath, Properties theProperties, String propComments) throws IOException {
    BufferedOutputStream bos = null;

    try {
        File file = new File(filepath);

        bos = new BufferedOutputStream(new FileOutputStream(file));

        theProperties.store(bos, propComments);
    } finally {
        if (bos != null) {
            bos.close();

            bos = null;
        }
    }
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * /*www .  j  a v  a2 s.  c om*/
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:com.mtt.myapp.common.util.FileDownloadUtil.java

/**
 * Provide file download from the given file path.
 * @param response {@link javax.servlet.http.HttpServletResponse}
 * @param desFile file path//from www . j ava  2s  . c  o m
 * @return true if succeeded
 */
public static boolean downloadFile(HttpServletResponse response, File desFile) {
    if (desFile == null || !desFile.exists()) {
        return false;
    }
    boolean result = true;
    response.reset();
    response.addHeader("Content-Disposition", "attachment;filename=" + desFile.getName());
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Length", "" + desFile.length());
    InputStream fis = null;
    byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE];
    OutputStream toClient = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(desFile));
        toClient = new BufferedOutputStream(response.getOutputStream());
        int readLength;
        while (((readLength = fis.read(buffer)) != -1)) {
            toClient.write(buffer, 0, readLength);
        }
        toClient.flush();
    } catch (FileNotFoundException e) {
        LOGGER.error("file not found:" + desFile.getAbsolutePath(), e);
        result = false;
    } catch (IOException e) {
        LOGGER.error("read file error:" + desFile.getAbsolutePath(), e);
        result = false;
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
    return result;
}