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:eu.optimis.sm.gui.utils.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {

    new File(fileObject.getParent()).mkdirs();
    new File(fileObject.getAbsolutePath()).delete();
    new File(fileObject.getAbsolutePath()).createNewFile();

    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;
        }/*from  ww  w  . j a  v  a  2s. co m*/
        streamOut.write(buf, 0, length);
    }

    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:Main.java

public static void copyFile(File src, File dest) {
    if (null == src || null == dest) {
        return;//from   w w  w  .  java  2s.c o  m
    }

    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = new FileInputStream(src);
        out = new FileOutputStream(dest);
        int byteCount = 8096;
        byte[] buffer = new byte[byteCount];
        int count = 0;
        while ((count = in.read(buffer, 0, byteCount)) != -1) {
            out.write(buffer, 0, count);
        }
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void copy(String source, String target, boolean isFolder) throws Exception {
    if (isFolder) {
        (new File(target)).mkdirs();
        File a = new File(source);
        String[] file = a.list();
        File temp = null;/*from  w  ww . j ava2s  . c  om*/
        for (int i = 0; i < file.length; i++) {
            if (source.endsWith(File.separator)) {
                temp = new File(source + file[i]);
            } else {
                temp = new File(source + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(target + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copy(source + "/" + file[i], target + "/" + file[i], true);
            }
        }
    } else {
        int byteread = 0;
        File oldfile = new File(source);
        if (oldfile.exists()) {
            InputStream inStream = new FileInputStream(source);
            File file = new File(target);
            file.getParentFile().mkdirs();
            file.createNewFile();
            FileOutputStream fs = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
        }
    }
}

From source file:com.frostwire.util.ZipUtils.java

private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time,
        ZipListener listener) throws IOException, FileNotFoundException {
    ZipEntry ze = null;//from www . j  a  v a  2s . c  o m

    int item = 0;

    while ((ze = zis.getNextEntry()) != null) {
        item++;

        String fileName = ze.getName();
        File newFile = new File(folder, fileName);

        LOG.debug("unzip: " + newFile.getAbsoluteFile());

        if (ze.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            continue;
        }

        if (listener != null) {
            int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount));
            listener.onUnzipping(fileName, progress);
        }

        FileOutputStream fos = new FileOutputStream(newFile);

        try {
            int n;
            byte[] buffer = new byte[1024];
            while ((n = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, n);

                if (listener != null && listener.isCanceled()) { // not the best way
                    throw new IOException("Uncompress operation cancelled");
                }
            }
        } finally {
            fos.close();
            zis.closeEntry();
        }

        newFile.setLastModified(time);
    }
}

From source file:Functions.FileUpload.java

public static boolean processFile(String path, FileItemStream item, String houseId, String owner) {
    if ("".equals(item.getName())) {
        return false;
    }//from w  w w  .ja  va  2s  . c o m
    String nm = houseId + item.getName();
    System.out.println("nm =====================" + nm);
    try {
        File f = new File(path + File.separator + "images/");

        if (!f.exists()) {//den uparxei kan o fakelos opote den einai sthn database tpt!
            System.out.println("Mphke sthn if den exists");
            f.mkdir();
            File savedFile = new File(f.getAbsoluteFile() + File.separator + nm);
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = -1;
            byte b[] = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
                //fos.write(x);
            }
            fos.flush();
            fos.close();
            database_common_functions.insertIntoBaseImage(houseId, nm);

            return true;
        } else {
            System.out.println("Mphke sthn if exists");
            if (database_common_functions.isHouseImageInDatabase(houseId, nm) == 1) {
                //overwrites current image with name nm.
                System.out.println("Mphke na kanei overwrite t arxeio");
                FileOutputStream fos = new FileOutputStream(f.getAbsoluteFile() + File.separator + nm);
                InputStream is = item.openStream();
                int x = -1;
                byte b[] = new byte[1024];
                while ((x = is.read(b)) != -1) {
                    fos.write(b, 0, x);
                    //fos.write(x);
                }
                fos.flush();
                fos.close();
                return true;
            } else {

                FileOutputStream fos = new FileOutputStream(f.getAbsoluteFile() + File.separator + nm);
                InputStream is = item.openStream();
                int x = -1;
                byte b[] = new byte[1024];
                while ((x = is.read(b)) != -1) {
                    fos.write(b, 0, x);
                    //fos.write(x);
                }
                fos.flush();
                fos.close();
                database_common_functions.insertIntoBaseImage(houseId, nm);
                return true;
            }

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

From source file:com.groupon.odo.proxylib.Utils.java

/**
 * Copies file from a resource to a local temp file
 *
 * @param sourceResource//  w  w w  .ja  v  a 2  s.  c om
 * @return Absolute filename of the temp file
 * @throws Exception
 */
public static File copyResourceToLocalFile(String sourceResource, String destFileName) throws Exception {
    try {
        Resource keystoreFile = new ClassPathResource(sourceResource);
        InputStream in = keystoreFile.getInputStream();

        File outKeyStoreFile = new File(destFileName);
        FileOutputStream fop = new FileOutputStream(outKeyStoreFile);
        byte[] buf = new byte[512];
        int num;
        while ((num = in.read(buf)) != -1) {
            fop.write(buf, 0, num);
        }
        fop.flush();
        fop.close();
        in.close();
        return outKeyStoreFile;
    } catch (IOException ioe) {
        throw new Exception("Could not copy keystore file: " + ioe.getMessage());
    }
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Unpacks an existing JAR/Zip archive./*from   w ww.j ava  2 s  . c o m*/
 * 
 * @param zipFile The Archive file to unzip.
 * @param outputFolder The destination folder where the unzipped content shall be saved.
 * @see <a href="http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/">
 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/</a>
 */
private static void unZipIt(File zipFile, File outputFolder) {

    byte[] buffer = new byte[1024];

    try {

        // create output directory is not exists
        File folder = outputFolder;
        if (folder.exists()) {
            FileUtils.deleteDirectory(folder);
        }
        folder.mkdir();

        // 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) {

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

            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            if (!ze.isDirectory()) {
                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();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static boolean copyFile(String quelle, String ziel) {
    if ((new File(quelle)).equals(new File(ziel))) {
        return false; // Quelle und Ziel sind dieselbe Datei!
    }//from w ww . j a  va  2s.  c  om
    final int BUFSIZE = 4096;
    FileInputStream f1;
    FileOutputStream f2;
    byte[] buf = new byte[BUFSIZE];
    int n;
    if (!canOpenFile(quelle)) {
        return false;
    }
    try {
        f1 = new FileInputStream(quelle);
        f2 = new FileOutputStream(ziel);
        while ((n = f1.read(buf, 0, BUFSIZE)) > 0) {
            f2.write(buf, 0, n);
        }
        f1.close();
        f2.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

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

public static File getResource(URI httpUri, String filename) throws IOException {
    /* download */
    HttpClient http = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(httpUri);//from w  ww .  j a v a  2 s. co  m
    HttpEntity entity = http.execute(request).getEntity();
    InputStream in = entity.getContent();
    File destination = new File(resourceDir, filename);
    FileOutputStream out = new FileOutputStream(destination);
    byte[] buf = new byte[1024];
    int read;
    while ((read = in.read(buf)) != -1) {
        out.write(buf, 0, read);
    }
    in.close();
    out.flush();
    out.close();
    return destination;
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * Download from given URL to given file location.
 * @param url String/*from  w  w w.  j a  va 2s . c  o m*/
 * @param filepath String
 * @return
 */
public static String download(String url, String filepath) {
    String status = "";
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpget);

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        if (filepath == null) {
            filepath = getFilePath(response); //NOSONAR
        }

        File file = new File(filepath);
        file.getParentFile().mkdirs();
        FileOutputStream fileout = new FileOutputStream(file);

        byte[] buffer = new byte[CACHE];
        int ch;
        while ((ch = is.read(buffer)) != -1) {
            fileout.write(buffer, 0, ch);
        }
        is.close();
        fileout.flush();
        fileout.close();
        status = Constant.DOWNLOADCSAR_SUCCESS;

    } catch (Exception e) {
        status = Constant.DOWNLOADCSAR_FAIL;
        LOG.error("Download csar file failed! " + e.getMessage(), e);
    }
    return status;
}