Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

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

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this file output stream.

Usage

From source file:Main.java

private static void copyResourceFile(Context context, int rid, String targetFile) throws IOException {
    InputStream fin = context.getResources().openRawResource(rid);
    FileOutputStream fos = new FileOutputStream(targetFile);

    int length;// w ww .  j  a  v  a  2  s .co m
    byte[] buffer = new byte[1024 * 32];
    while ((length = fin.read(buffer)) != -1) {
        fos.write(buffer, 0, length);
    }
    fin.close();
    fos.close();
}

From source file:Main.java

public static void unzip(final InputStream input, final File destFolder) {
    try {//w w  w .  j  av  a 2s.c  om
        byte[] buffer = new byte[4096];
        int read;
        ZipInputStream is = new ZipInputStream(input);
        ZipEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                File fileFolder = destFolder;
                int lastSep = entry.getName().lastIndexOf(File.separatorChar);
                if (lastSep != -1) {
                    String dirPath = fileName.substring(0, lastSep);
                    fileFolder = new File(fileFolder, dirPath);
                    fileName = fileName.substring(lastSep + 1);
                }
                fileFolder.mkdirs();
                File file = new File(fileFolder, fileName);
                FileOutputStream os = new FileOutputStream(file);
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
                os.close();
            }
        }
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex);
    }
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//from www . j av a 2 s  .c  o  m
    } else {
        file.delete();
    }

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {//from www  .j  av  a  2 s .c  o m
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();/*from   w  w w.j a  va2s  .  c  o  m*/

    if (file != null && file.exists())
        file.delete();

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static void Stream2File(InputStream is, File file) {
    byte[] b = new byte[1024];
    int len;/*from   w ww.  j a v a 2s  . c  om*/
    FileOutputStream os = null;
    try {
        os = new FileOutputStream(file);
        while ((len = is.read(b)) != -1) {
            os.write(b, 0, len);
            os.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is, os);
    }
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(fromFile);
    FileOutputStream outputStream = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    for (int numRead; (numRead = inputStream.read(buffer)) != -1;)
        outputStream.write(buffer, 0, numRead);
    inputStream.close();//ww  w . j  a  v  a  2 s.c o m
    outputStream.close();
}

From source file:Main.java

public static void copyToSD(InputStream in, String fileName) {
    String path = Environment.getExternalStorageDirectory() + File.separator + fileName;
    File file = new File(path);
    try {// www.ja  v  a2 s .  com
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static final void downloadBundle(InputStream is, String saveFilePath) throws IOException {
    checkDir(saveFilePath);//from  w w w  .j  ava  2 s . c  o m
    File file = new File(saveFilePath, ZIP_BUNDLE_NAME);
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream outputStream = new FileOutputStream(file, false);

    byte[] buf = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) {
        outputStream.write(buf, 0, len);
    }
    outputStream.close();
    is.close();
}

From source file:Main.java

/**
 * Copies source file to destination.// w ww . j  av  a  2  s .  c  o  m
 * If destination file exists it will be overwritten silently.
 *
 * @param source file
 * @param target file
 */
public static void copyFile(final File source, final File target) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(target);
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}