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:com.ggasoftware.uitest.utils.FileUtil.java

/**
 * Check file download from url./*from   w w  w.ja  v  a 2 s .c om*/
 *
 * @param downloadUrl    - url of file to download
 * @param outputFilePath - file path for output
 * @throws Exception - exception
 */
public static void downloadFile(String downloadUrl, String outputFilePath) throws IOException {

    ReporterNGExt.logAction("", "", String.format("Download file form url: %s", downloadUrl));

    CookieStore cookieStore = seleniumCookiesToCookieStore();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.setCookieStore(cookieStore);
    HttpGet httpGet = new HttpGet(downloadUrl);
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        File outputFile = new File(outputFilePath);
        InputStream inputStream = entity.getContent();
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        int read;
        byte[] bytes = new byte[1024];
        while ((read = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, read);
        }
        fileOutputStream.close();
        ReporterNGExt.logTechnical(
                String.format("downloadFile: %d bytes. %s", outputFile.length(), entity.getContentType()));
    } else {
        ReporterNGExt.logFailedScreenshot(ReporterNGExt.BUSINESS_LEVEL, "Download failed!");
    }
}

From source file:Main.java

public static File writeAssetToFile(Context ctx, String assetName, String destinationFile) throws IOException {
    File destination = new File(destinationFile);
    if (!destination.getParentFile().exists())
        destination.getParentFile().mkdirs();
    if (destination.exists())
        destination.delete();//from   w w w  .j  a  v a 2  s .com
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = ctx.getAssets().open(assetName);
        fos = new FileOutputStream(destination);
        int read;
        byte[] buf = new byte[65536];
        while ((read = is.read(buf, 0, buf.length)) != -1) {
            fos.write(buf, 0, read);
        }
    } finally {
        if (is != null)
            is.close();
        if (fos != null)
            fos.close();
    }
    return destination;
}

From source file:gabi.FileUploadServlet.java

private static void unzip(String zipFilePath, String destDir) {
    File dir = new File(destDir);
    // create output directory if it doesn't exist
    if (!dir.exists())
        dir.mkdirs();/*from   ww  w  .j a  v  a 2s. c  o m*/
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFilePath);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            System.out.println("Unzipping to " + newFile.getAbsolutePath());
            //create directories for sub directories in zip
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static byte[] copyFile(InputStream is, FileOutputStream os) throws IOException {
    MessageDigest digester = null;
    try {/*from w  w  w. j  a  v a2 s. com*/
        digester = MessageDigest.getInstance("MD5");

        byte[] buffer = new byte[16 * 1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
            digester.update(buffer, 0, length);
        }
        os.flush();
        os.close();

        //os.getFD().sync();
        //Log.d(TAG, "done copying");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        is.close();
        Log.d(TAG, "done copying");
    }
    if (digester != null) {
        byte[] digest = digester.digest();
        return digest;
    }

    return null;
}

From source file:Main.java

public static boolean fileCopy(String from, String to) {
    boolean result = false;

    int size = 1 * 1024;

    FileInputStream in = null;/*from   w  ww  .  ja v  a  2  s. co  m*/
    FileOutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        byte[] buffer = new byte[size];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        result = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
        }
    }
    return result;
}

From source file:FileCopy.java

public static void copy(String fromFileName, String toFileName) throws IOException {
    File fromFile = new File(fromFileName);
    File toFile = new File(toFileName);

    if (!fromFile.exists())
        throw new IOException("FileCopy: " + "no such source file: " + fromFileName);
    if (!fromFile.isFile())
        throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName);
    if (!fromFile.canRead())
        throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName);

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new IOException("FileCopy: " + "destination file is unwriteable: " + toFileName);
        System.out.print("Overwrite existing file " + toFile.getName() + "? (Y/N): ");
        System.out.flush();// w w  w. j  a  v a 2  s . c o m
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String response = in.readLine();
        if (!response.equals("Y") && !response.equals("y"))
            throw new IOException("FileCopy: " + "existing file was not overwritten.");
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new IOException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:org.meshpoint.anode.util.ModuleUtils.java

public static boolean copyFile(File src, File dest) {
    boolean result = true;
    if (src.isDirectory()) {
        result = copyDir(src, dest);/*  w w w .  ja v  a  2 s .  co  m*/
    } else {
        try {
            int count;
            byte[] buf = new byte[1024];
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(dest);
            while ((count = fis.read(buf, 0, 1024)) != -1)
                fos.write(buf, 0, count);
        } catch (IOException e) {
            Log.v(TAG, "moveFile exception: aborting; exception: " + e + "; src = " + src.toString()
                    + "; dest = " + dest.toString());
            return false;
        }
    }
    return result;
}

From source file:co.marcin.novaguilds.util.IOUtils.java

public static void saveInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream outputStream = null;
    try {//  ww  w. ja v  a2s.c  o  m
        outputStream = new FileOutputStream(file);

        int read;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:Main.java

public static void copyInputToFile(InputStream in, String path) {
    BufferedInputStream bis = null;
    FileOutputStream fos = null;
    try {//from   w w  w .j a va  2  s  .c  o  m
        byte[] buffer = new byte[10 * 1024];
        bis = new BufferedInputStream(in);
        fos = new FileOutputStream(path);
        int a = bis.read(buffer, 0, buffer.length);
        while (a != -1) {
            fos.write(buffer, 0, a);
            fos.flush();
            a = bis.read(buffer, 0, buffer.length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeOutputStream(fos);
        closeInputStream(bis);
        closeInputStream(in);
    }
}

From source file:Main.java

private static void copyFileFromAssets(Context context, String destPathName, String fileName) {

    File destFile = new File(destPathName);

    if (!destFile.exists()) {

        try {//from   w w w  . j  a va2s  .c o m

            ///
            InputStream inputStream = context.getAssets().open(fileName);

            FileOutputStream outputStream = new FileOutputStream(destPathName);

            ///
            byte[] i_buffer = new byte[1024];

            for (;;) {

                int length = inputStream.read(i_buffer);

                if (length > 0) {

                    outputStream.write(i_buffer, 0, length);

                } else {

                    break;
                }
            }

            ///
            inputStream.close();

            outputStream.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}