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 unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//from   ww  w. j  ava 2  s  .c  o m
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void inputToOutput(FileOutputStream outputStream, InputStream inputStream) throws IOException {
    byte[] buffer = new byte[1024];
    int len;//w w w  .  j a v  a2  s. c  o  m
    while ((len = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, len);
    }
    outputStream.close();
    inputStream.close();
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    Log.d(TAG, "write file=====start==");
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();/*from  ww w .  ja va 2  s  . 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);
    }
    Log.d(TAG, "write file====success===");
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static void copyFile(String oldPath, String newPath) {
    try {// ww w. ja  v  a  2 s.co m
        int byteRead;
        File oldFile = new File(oldPath);
        if (oldFile.exists()) {
            InputStream inStream = new FileInputStream(oldPath);
            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            inStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void decompressGzipFile(String gzipFile) {
    try {/*from  ww  w . j  av a  2s.  c  o m*/
        File newFile = new File(Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        FileInputStream fis = new FileInputStream(gzipFile);
        GZIPInputStream gis = new GZIPInputStream(fis);
        FileOutputStream fos = new FileOutputStream(newFile);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //close resources
        fos.close();
        gis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static long write(String fileName, String data, int position) throws FileNotFoundException, IOException {
    boolean append = false;
    if (position > 0) {
        truncateFile(fileName, position);
        append = true;/* w  ww . j  a  va2  s.c  o m*/
    }

    byte[] rawData = data.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    FileOutputStream out = new FileOutputStream(fileName, append);
    byte buff[] = new byte[rawData.length];
    in.read(buff, 0, buff.length);
    out.write(buff, 0, rawData.length);
    out.flush();
    out.close();

    return data.length();
}

From source file:Main.java

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

From source file:Main.java

public static void WriteFile(InputStream stream, String fileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileName);
    byte[] buff = new byte[1024];
    int readLen;//from w w w  .j a va  2 s  .c o  m
    do {
        readLen = stream.read(buff);
        if (readLen > 0)
            fos.write(buff, 0, readLen);
    } while (readLen > 0);

    fos.close();
}

From source file:Main.java

/**
 * Uncompress gzipped files// w  w  w.j a  va 2s.com
 * @param gzippedFile The file to uncompress
 * @param destinationFile The resulting file
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static void gunzip(File gzippedFile, File destinationFile) throws IOException {
    int buffer = 2048;

    FileInputStream in = new FileInputStream(gzippedFile);
    GZIPInputStream zipin = new GZIPInputStream(in);

    byte[] data = new byte[buffer];

    // decompress the file
    FileOutputStream out = new FileOutputStream(destinationFile);
    try {
        int length;
        while ((length = zipin.read(data, 0, buffer)) != -1)
            out.write(data, 0, length);
    } finally {
        out.close();

        zipin.close();
        in.close();
    }

}

From source file:Main.java

public static Boolean copyFileToSd(InputStream assetFile, File sdFile) {
    boolean flags = false;
    try {// w ww . j  ava2s .  co  m
        FileOutputStream fos = new FileOutputStream(sdFile);
        byte[] buffer = new byte[1024];
        int count;
        while ((count = assetFile.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        flags = true;
        fos.close();
        assetFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return flags;
}