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 unZipFolder(InputStream input, String outPathString) throws Exception {
    java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);
    java.util.zip.ZipEntry zipEntry = null;
    String szName = "";

    while ((zipEntry = inZip.getNextEntry()) != null) {
        szName = zipEntry.getName();/* ww  w.j a v a2  s  .com*/

        if (zipEntry.isDirectory()) {

            // get the folder name of the widget
            szName = szName.substring(0, szName.length() - 1);
            java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
            folder.mkdirs();

        } else {

            java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
            file.createNewFile();
            // get the output stream of the file
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
            int len;
            byte[] buffer = new byte[1024];
            // read (len) bytes into buffer
            while ((len = inZip.read(buffer)) != -1) {
                // write (len) byte from buffer at the position 0
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        }
    } //end of while

    inZip.close();
}

From source file:net.emotivecloud.scheduler.drp4one.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.debug("File " + fileObject.getAbsolutePath() + " didn't exist. Creating one with default values...");
    System.out.println(//from   w w  w .  j a v  a2  s.  c o m
            "File " + fileObject.getAbsolutePath() + " didn't exist. Creating one with default values...");

    //Create parent directories.
    log.debug("Creating parent directories.");
    new File(fileObject.getParent()).mkdirs();

    //Create an empty file to copy the contents of the default file.
    log.debug("Creating empty file.");
    new File(fileObject.getAbsolutePath()).createNewFile();

    //Copy file.
    log.debug("Copying file " + fileObject.getName());
    InputStream streamIn = ConfigManager.class.getResourceAsStream("/" + fileObject.getName());
    FileOutputStream streamOut = new FileOutputStream(fileObject.getAbsolutePath());
    byte[] buf = new byte[8192];
    while (true) {
        int length = streamIn.read(buf);
        if (length < 0) {
            break;
        }
        streamOut.write(buf, 0, length);
    }

    //Close streams after copying.
    try {
        streamIn.close();
    } catch (IOException ex) {
        log.error("Couldn't close input stream");
        log.error(ex.getMessage());
    }
    try {
        streamOut.close();
    } catch (IOException ex) {
        log.error("Couldn't close file output stream");
        log.error(ex.getMessage());
    }
}

From source file:de.pksoftware.springstrap.sapi.util.WebappZipper.java

/**
 * Unzip it/*from w  w w . j  a  v  a2  s  .com*/
 * @param zipFile input zip file
 * @param output zip file output folder
 */
public static void unzip(String zipFile, String outputFolder) {
    logger.info("Unzipping {} into {}.", zipFile, outputFolder);

    byte[] buffer = new byte[1024];

    try {

        //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) {
            if (ze.isDirectory()) {
                ze = zis.getNextEntry();
                continue;
            }

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

            logger.debug("Unzipping: {}", newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            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();
            ze = zis.getNextEntry();
        }

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

        logger.debug("Unzipping {} completed.", zipFile);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.kegbot.app.util.Downloader.java

/**
 * Downloads an HTTP resource to an output file.
 *
 * @param url    the resource to download
 * @param output the output file/*from   w ww.j a  v  a 2  s.  c om*/
 * @throws IOException upon any error
 */
public static void downloadRaw(final String url, final File output) throws IOException {
    final URL destUrl = new URL(url);
    final HttpURLConnection connection = (HttpURLConnection) destUrl.openConnection();

    final FileOutputStream out = new FileOutputStream(output);
    final InputStream input = connection.getInputStream();

    try {
        byte buffer[] = new byte[4096];
        int len;
        while ((len = input.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }
    } finally {
        out.close();
        input.close();
    }

}

From source file:Main.java

public static void unCompress(String zipPath, String toPath) throws IOException {
    File zipfile = new File(zipPath);
    if (!zipfile.exists())
        return;/*w  ww . j av a  2 s.  co  m*/

    if (!toPath.endsWith("/"))
        toPath += "/";

    File destFile = new File(toPath);
    if (!destFile.exists())
        destFile.mkdirs();

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
    ZipEntry entry = null;

    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File file = new File(toPath + entry.getName() + "/");
                file.mkdirs();
            } else {
                File file = new File(toPath + entry.getName());
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdirs();

                FileOutputStream fos = null;

                try {
                    fos = new FileOutputStream(file);
                    byte buf[] = new byte[1024];
                    int len = -1;
                    while ((len = zis.read(buf, 0, 1024)) != -1) {
                        fos.write(buf, 0, len);
                    }
                } finally {

                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }

    } finally {
        zis.close();
    }

}

From source file:Main.java

private static void copyAsset(AssetManager am, File rep, boolean force) {
    try {// www  .j a  va2  s .c o m
        String assets[] = am.list("");
        for (int i = 0; i < assets.length; i++) {
            boolean hp48 = assets[i].equals("hp48");
            boolean hp48s = assets[i].equals("hp48s");
            boolean ram = assets[i].equals("ram");
            boolean rom = assets[i].equals("rom");
            boolean rams = assets[i].equals("rams");
            boolean roms = assets[i].equals("roms");
            int required = 0;
            if (ram)
                required = 131072;
            else if (rams)
                required = 32768;
            else if (rom)
                required = 524288;
            else if (roms)
                required = 262144;
            //boolean SKUNK = assets[i].equals("SKUNK");
            if (hp48 || rom || ram || hp48s || roms || rams) {
                File fout = new File(rep, assets[i]);
                if (!fout.exists() || fout.length() == 0 || (required > 0 && fout.length() != required)
                        || force) {
                    Log.i("x48", "Overwriting " + assets[i]);
                    FileOutputStream out = new FileOutputStream(fout);
                    InputStream in = am.open(assets[i]);
                    byte buffer[] = new byte[8192];
                    int n = -1;
                    while ((n = in.read(buffer)) > -1) {
                        out.write(buffer, 0, n);
                    }
                    out.close();
                    in.close();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean installDll(Context context) {
    boolean isSuccess = false;
    File file = context.getFileStreamPath("Disdll.dll");
    boolean isDeleteSuccess = true;
    if (file.exists()) {
        isDeleteSuccess = file.delete();
    }//from  ww  w.  ja  v  a2  s.c  o  m
    if (isDeleteSuccess) {
        try {
            FileOutputStream outputStream = context.openFileOutput("Disdll.dll", Context.MODE_PRIVATE);
            InputStream inputStream = context.getAssets().open("Disdll.dll");
            byte[] temp = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(temp)) != -1) {
                outputStream.write(temp, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        isSuccess = checkIfDllInstalled(context);
    }
    return isSuccess;
}

From source file:Main.java

public static void Unzip(String dir, byte[] data) throws Exception {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ZipInputStream zipIn = new ZipInputStream(input);
    ZipEntry entry;/*  www .  j  ava  2s.  c  o  m*/
    while ((entry = zipIn.getNextEntry()) != null) {
        String outpath = dir + entry.getName();
        FileOutputStream output = null;
        try {
            File f = new File(outpath);
            f.getParentFile().mkdirs();

            output = new FileOutputStream(f);
            int len = 0;
            byte[] buffer = new byte[4096];
            while ((len = zipIn.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

    zipIn.close();
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void write(InputStream tarGzInputStream, Path outTarGz) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream);
    FileOutputStream fios = new FileOutputStream(outTarGz.toFile());
    int buffersize = 1024;
    final byte[] buffer = new byte[buffersize];
    int n = 0;/*from  w  ww. ja  v  a 2  s.  com*/
    while (-1 != (n = gzipStream.read(buffer))) {
        fios.write(buffer, 0, n);
    }
    fios.close();
    gzipStream.close();
}

From source file:com.nokia.helium.diamonds.DiamondsSessionSocket.java

/**
 * Write the content of an inputstream into a temp file. Deletion of the file
 * has to be made by the caller./*w ww.ja va  2 s .com*/
 * @param stream the input stream
 * @return the temp file created.
 */
protected static File streamToTempFile(InputStream stream) throws IOException {
    File temp = File.createTempFile("diamonds", "xml");
    FileOutputStream out = new FileOutputStream(temp);
    int read = 0;
    byte[] bytes = new byte[1024];

    while ((read = stream.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }
    out.flush();
    out.close();
    stream.close();
    return temp;
}