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

/**
 * This method copies a binary from asset directory to working directory.
 * //w w  w.  j  ava  2  s. c o  m
 * @param assetPath
 * @param localPath
 * @param context
 * @return true == copied, false == text busy
 */
private static boolean copyFile(String assetPath, String localPath, Context context) {
    try {
        // detect architecture
        if (isARM())
            assetPath += "_arm";
        else if (isX86())
            assetPath += "_x86";
        else if (isMIPS())
            assetPath += "_mips";
        else
            assetPath += "_arm";

        InputStream binary = context.getAssets().open(assetPath);
        FileOutputStream execute = new FileOutputStream(localPath);

        int read = 0;
        byte[] buffer = new byte[4096];

        while ((read = binary.read(buffer)) > 0) {
            execute.write(buffer, 0, read);
        }

        execute.close();
        binary.close();

        execute = null;
        binary = null;

    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:com.fun.util.TesseractUtil.java

/**
 * url?/*  w  w  w  .j ava2  s .  com*/
 *
 * @param url
 * @return
 * @throws IOException
 */
private static File getFileFromUrl(URL url) throws IOException {
    File tmpImage = File.createTempFile("tesseract-ocr-download", null);
    InputStream in = url.openConnection().getInputStream();
    FileOutputStream fos = new FileOutputStream(tmpImage);
    byte[] buf = new byte[1024];
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        fos.write(buf, 0, len);
    }
    fos.flush();
    fos.close();
    return tmpImage;
}

From source file:Main.java

public static boolean copyFileTo(File srcFile, File destFile) throws IOException {
    if (srcFile == null || destFile == null) {
        return false;
    }//from w w  w . j  a va2 s .c o  m
    if (srcFile.isDirectory() || destFile.isDirectory())
        return false;
    if (!srcFile.exists()) {
        return false;
    }
    if (!destFile.exists()) {
        createFile(destFile.getAbsolutePath());
    }
    FileInputStream fis = new FileInputStream(srcFile);
    FileOutputStream fos = new FileOutputStream(destFile);
    int readLen = 0;
    byte[] buf = new byte[1024];
    while ((readLen = fis.read(buf)) != -1) {
        fos.write(buf, 0, readLen);
    }
    fos.flush();
    fos.close();
    fis.close();
    return true;
}

From source file:Main.java

public static void copyDirFile(String oldPath, String newPath) {
    try {//from  w  w  w  .  j  av a2s .com
        new File(newPath).mkdirs();
        File a = new File(oldPath);
        System.out.println("oldPath:" + oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyDirFile(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:br.edu.ifpb.sislivros.model.RequisicaoDeImg.java

public static void inserirImagem(FileItem item, String realPath, String nomeDaImagem) throws IOException {

    //Pegar o diretorio /imagensPerfil dentro do diretorio atual
    String diretorio = realPath + "/";

    //Criar diretorio caso no exista;
    File f = new File(diretorio);

    if (!f.exists()) {
        f.mkdir();/*from  ww  w  .j av  a2 s  . c  o  m*/
    }

    //Mandar o arquivo para o diretorio informado
    f = new File(diretorio + nomeDaImagem + ".jpg");

    try {
        FileOutputStream output = new FileOutputStream(f);
        InputStream is = item.getInputStream();

        byte[] buffer = new byte[2048];

        int nLidos;

        while ((nLidos = is.read(buffer)) >= 0) {
            output.write(buffer, 0, nLidos);
        }

        output.flush();
    } finally {

    }

}

From source file:Main.java

public static boolean saveFile(String filePath, InputStream inputStream) throws IOException {
    boolean result = false;
    if (filePath != null && inputStream != null) {
        Log.d(TAG, "filePath:" + filePath);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();/*from   w  w  w . j a  v  a 2 s  . c o m*/
        }
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
            byte[] buf = new byte[1024];
            int size = 0;
            while ((size = inputStream.read(buf, 0, 1024)) != -1) {
                fos.write(buf, 0, size);
            }

            fos.flush();
            fos.close();
            inputStream.close();
            result = true;
        }
    }
    return result;
}

From source file:Main.java

public static boolean copyFileTo(InputStream inputStream, File destFile) throws IOException {
    if (inputStream == null || destFile == null) {
        return false;
    }/*from  w  ww.j a v  a2 s .c o m*/
    if (destFile.isDirectory())
        return false;

    if (!destFile.exists()) {
        createFile(destFile.getAbsolutePath());
    }
    FileOutputStream fos = new FileOutputStream(destFile);
    int readLen = 0;
    byte[] buf = new byte[1024];
    while ((readLen = inputStream.read(buf)) != -1) {
        fos.write(buf, 0, readLen);
    }
    fos.flush();
    fos.close();
    inputStream.close();
    return true;
}

From source file:Main.java

public static String assetToSd(Context ctx, String inFileName, String outFileName) {

    try {/*  w  w  w .  j  av  a2 s.  c  o  m*/
        InputStream is = ctx.getAssets().open(inFileName);
        if (is == null) {
            return null;
        }

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = sd + "/uexTencent/";
            File filePath = new File(path);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String fileName = path + outFileName;
            FileOutputStream fos = new FileOutputStream(fileName);

            byte[] buf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = is.read(buf)) != -1) {
                fos.write(buf, 0, len);
                total++;
            }
            is.close();
            fos.close();
            fileSize = total;
            return fileName;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:msec.org.GzipUtil.java

static public void unzip(String srcFile) throws Exception {
    GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(srcFile));
    int index = srcFile.indexOf(".gz");
    String destFile = "";
    if (index == srcFile.length() - 3) {
        destFile = srcFile.substring(0, index);
    } else {/*from www .  j a  v a2 s.c  om*/
        destFile = srcFile + ".decompress";
    }
    FileOutputStream out = new FileOutputStream(destFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:Main.java

public static void copyFromPackage(Context context, int ressourceId, String target) throws IOException {
    FileOutputStream lOutputStream = context.openFileOutput(target, 0);
    InputStream lInputStream = context.getResources().openRawResource(ressourceId);
    int readByte;
    byte[] buff = new byte[8048];
    while ((readByte = lInputStream.read(buff)) != -1) {
        lOutputStream.write(buff, 0, readByte);
    }// w  w w  .  j a  v a 2  s.c  om
    lOutputStream.flush();
    lOutputStream.close();
    lInputStream.close();
}