Android File Write writeFile(File file, byte[] data)

Here you can find the source of writeFile(File file, byte[] data)

Description

write File

Declaration

public static void writeFile(File file, byte[] data) throws Exception 

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {
    private final static int BUFFER = 8192;

    public static void writeFile(File file, byte[] data) throws Exception {
        DataOutputStream out = null;
        if (null != file && null != data) {
            try {
                out = new DataOutputStream(new FileOutputStream(file));
                out.write(data);//  w ww  .  j  a v  a2  s  . com
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }

    public static int writeFile(File file, InputStream inStream)
            throws IOException {
        long dataSize = 0;
        DataInputStream in = null;
        DataOutputStream out = null;
        if (null != inStream && null != file) {
            try {
                byte buffer[] = new byte[BUFFER];
                out = new DataOutputStream(new FileOutputStream(file));
                in = new DataInputStream(inStream);

                int nbyteread;
                while ((nbyteread = in.read(buffer)) != -1) {
                    out.write(buffer, 0, nbyteread);
                    dataSize += nbyteread;
                }
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
        }

        return (int) (dataSize / 1024);
    }
}

Related

  1. writeContatctInfo(String accountId, Vector contactInfo, File contactInfoFile)
  2. writeFile(File file, String data)
  3. writeInstallationFile(File installation)
  4. writeOneLine(String filename, String value)
  5. stringToFile(String filename, String stringToWrite)