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

public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
        return;//from   www . j  a  v a 2 s .co m
    }

    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }

    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        FileInputStream fosfrom = new FileInputStream(fromFile);
        FileOutputStream fosto = new FileOutputStream(toFile);

        byte[] bt = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

private static boolean copyFile(File assetFile, File storageFile, AssetManager assetManager) {
    try {//from   w  w  w  .j  av  a  2s  .com
        InputStream in = assetManager.open(assetFile.getPath());
        FileOutputStream out = new FileOutputStream(storageFile);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.close();
        return true;
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();//from www .  j ava  2 s  .  c  om
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();

}

From source file:Util.java

public static File copyFromStreamToFile(InputStream is, File file) {
    FileOutputStream out = null;
    try {/*  w  ww . j  a  v  a2  s .c  o m*/
        out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

    } catch (Exception ex) {

    } finally {
        try {
            is.close();
            out.close();
        } catch (IOException ex) {

        }
    }
    return file;
}

From source file:Main.java

public static void copyAssets(Context context, String assetsName, String destFilePath) throws IOException {
    File file = new File(destFilePath);
    FileOutputStream out = new FileOutputStream(file);

    InputStream in = context.getAssets().open(assetsName);

    byte[] buf = new byte[1024];
    int len;//  w w  w. jav a  2s .  c o m
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void copyBitmapFromStream(InputStream in, File file) {
    FileOutputStream fout = null;
    try {//from   w ww.j  av  a2 s.co m
        fout = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            fout.write(buffer, 0, len);
        }
        fout.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void unzip(InputStream is, String dir) throws IOException {
    File dest = new File(dir);
    if (!dest.exists()) {
        dest.mkdirs();/*from  w  w  w  .j  av a2 s  .  c om*/
    }

    if (!dest.isDirectory())
        throw new IOException("Invalid Unzip destination " + dest);
    if (null == is) {
        throw new IOException("InputStream is null");
    }

    ZipInputStream zip = new ZipInputStream(is);

    ZipEntry ze;
    while ((ze = zip.getNextEntry()) != null) {
        final String path = dest.getAbsolutePath() + File.separator + ze.getName();

        String zeName = ze.getName();
        char cTail = zeName.charAt(zeName.length() - 1);
        if (cTail == File.separatorChar) {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new IOException("Unable to create folder " + file);
                }
            }
            continue;
        }

        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = new byte[1024];
        int c;
        while ((c = zip.read(bytes)) != -1) {
            fout.write(bytes, 0, c);
        }
        zip.closeEntry();
        fout.close();
    }
}

From source file:Main.java

public static File saveStreamToFile(InputStream inputStream, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();/*from  w w  w.j  a  v a2s  . c  o m*/
    }
    try {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(path);

        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

public static void copyFile(File targetFile, File file) {
    if (targetFile.exists()) {
        return;//from   www . j av a2  s.c  o  m
    } else {
        createFile(targetFile, true);
    }
    try {
        FileInputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024 * 5];
        int len;
        while ((len = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void streamToFile(InputStream is, File targetFile) throws IOException {
    FileOutputStream fos = new FileOutputStream(targetFile);
    byte[] bytes = new byte[1024];
    while (true) {
        int n = is.read(bytes);
        if (n < 0) {
            break;
        }//from  ww w  .j  av a  2 s . c  o m
        fos.write(bytes, 0, n);
    }
    is.close();
    fos.close();
}