Java FileOutputStream Write copyFileFromAssets(InputStream inputStream, String pathToWrite)

Here you can find the source of copyFileFromAssets(InputStream inputStream, String pathToWrite)

Description

copy File From Assets

License

Apache License

Declaration

public static void copyFileFromAssets(InputStream inputStream,
            String pathToWrite) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static void copyFileFromAssets(InputStream inputStream,
            String pathToWrite) {
        OutputStream outputStream = null;

        try {//from ww  w .  j  av  a  2  s . c  o  m
            File newFile = new File(pathToWrite);
            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            // write the inputStream to a FileOutputStream
            outputStream = new FileOutputStream(new File(pathToWrite));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    // outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

Related

  1. copyFile(InputStream in, File destination)
  2. copyFile(InputStream input, File outFile)
  3. copyFile(InputStream input, File outputLocation)
  4. copyFile(InputStream input, OutputStream output)
  5. copyFile(InputStream sourceFileIs, File destDirFile, String destFileName)
  6. copyFileUsingFileStreams(InputStream source, File dest)
  7. copyFileUsingStream(InputStream sourceStream, File dest)
  8. save(byte[] bs, String fn)
  9. save(byte[] bytes, File path)