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.stratosphere.yarn.Utils.java

public static void copyJarContents(String prefix, String pathToJar) throws IOException {
    LOG.info("Copying jar (location: " + pathToJar + ") to prefix " + prefix);

    JarFile jar = null;/*w w w  .java 2  s. c o  m*/
    jar = new JarFile(pathToJar);
    Enumeration<JarEntry> enumr = jar.entries();
    byte[] bytes = new byte[1024];
    while (enumr.hasMoreElements()) {
        JarEntry entry = enumr.nextElement();
        if (entry.getName().startsWith(prefix)) {
            if (entry.isDirectory()) {
                File cr = new File(entry.getName());
                cr.mkdirs();
                continue;
            }
            InputStream inStream = jar.getInputStream(entry);
            File outFile = new File(entry.getName());
            if (outFile.exists()) {
                throw new RuntimeException("File unexpectedly exists");
            }
            FileOutputStream outputStream = new FileOutputStream(outFile);
            int read = 0;
            while ((read = inStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            inStream.close();
            outputStream.close();
        }
    }
    jar.close();
}

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

public static void unpackArchive(File theFile, File targetDir, IProgressMonitor monitor, Set<String> cpaths,
        Set<String> lpaths) {
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;//w  w w. j a v  a  2s .c om
    try {
        zis = new ZipInputStream(new FileInputStream(theFile));

        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            File file = new File(targetDir, File.separator + ze.getName());

            new File(file.getParent()).mkdirs();
            String fname = file.getName();
            if (ze.isDirectory()) {
                if (fname.equals("src"))
                    cpaths.add(ze.getName());
            } else {
                FileOutputStream fos = new FileOutputStream(file);
                int len;
                while ((len = zis.read(buffer)) > 0)
                    fos.write(buffer, 0, len);

                fos.close();
            }
            if (file.getParentFile().getName().equals("lib")
                    && (fname.endsWith(".jar") || fname.endsWith(".zip")))
                lpaths.add(ze.getName());
            if (monitor.isCanceled()) {
                zis.close();
                return;
            }
            ze = zis.getNextEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:localization.split.java

public static Vector<String> readzipfile(String filepath) {
    Vector<String> v = new Vector<String>();
    byte[] buffer = new byte[1024];
    String outputFolder = filepath.substring(0, filepath.lastIndexOf("."));
    System.out.println(outputFolder);
    try {/* ww w .  j  a  v a  2 s . c o  m*/
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        ZipInputStream zis = new ZipInputStream(new FileInputStream(filepath));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + "\\" + fileName);
            v.addElement(newFile.getAbsolutePath());
            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 (Exception e) {

    }
    return v;
}

From source file:eu.optimis.ics.core.util.PropertiesReader.java

/**
 * Creates a default properties file if it doesn't exist
 * @param fileObject    properties file//  w  w w.j a  va 2s.com
 * @throws Exception  Unexcepted error occurs
 */
private static void createDefaultConfigFile(File fileObject) throws Exception {

    log.info("ics.core.util.PropertiesReader.createDefaultConfigFile(): File " + fileObject.getAbsolutePath()
            + " doesn't exist. Creating one with default values...");

    // Create parent directories.
    log.info("ics.core.util.PropertiesReader.createDefaultConfigFile(): Creating parent directories.");
    new File(fileObject.getParent()).mkdirs();

    // Create an empty file to copy the contents of the default file.
    log.info("ics.core.util.PropertiesReader.createDefaultConfigFile(): Creating empty file.");
    new File(fileObject.getAbsolutePath()).createNewFile();

    // Copy file.
    log.info("ics.core.util.PropertiesReader.createDefaultConfigFile(): Copying file " + fileObject.getName());
    InputStream streamIn = PropertiesReader.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 ignore) {
        log.error("ics.core.util.PropertiesReader.createDefaultConfigFile(): Couldn't close input stream");
    }

    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error(
                "ics.core.util.PropertiesReader.createDefaultConfigFile(): Couldn't close file output stream");
    }
}

From source file:Main.java

public static boolean copyFile(File from, File to, byte[] buf) {
    if (buf == null)
        buf = new byte[BUFFER_SIZE];

    ///*from  w ww . j av a 2s .c om*/
    // System.out.println("Copy file ("+from+","+to+")");
    FileInputStream from_s = null;
    FileOutputStream to_s = null;

    try {
        from_s = new FileInputStream(from);
        to_s = new FileOutputStream(to);

        for (int bytesRead = from_s.read(buf); bytesRead != -1; bytesRead = from_s.read(buf))
            to_s.write(buf, 0, bytesRead);

        from_s.close();
        from_s = null;

        to_s.getFD().sync(); // RESOLVE: sync or no sync?
        to_s.close();
        to_s = null;
    } catch (IOException ioe) {
        return false;
    } finally {
        if (from_s != null) {
            try {
                from_s.close();
            } catch (IOException ioe) {
            }
        }
        if (to_s != null) {
            try {
                to_s.close();
            } catch (IOException ioe) {
            }
        }
    }

    return true;
}

From source file:at.uni_salzburg.cs.ckgroup.pilot.sensor.OpenStreetMapTileCache.java

public static void downloadFile(String url, File file) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*from w ww  .j a va 2 s  .co m*/

    response = httpclient.execute(httpget);
    FileOutputStream outStream = new FileOutputStream(file);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream inStream = entity.getContent();
        int l;
        byte[] tmp = new byte[8096];
        while ((l = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, l);
        }
    }
}

From source file:com.example.prathik1.drfarm.OCRRestAPI.java

private static void DownloadConvertedFile(String outputFileUrl) throws IOException {
        URL downloadUrl = new URL(outputFileUrl);
        HttpURLConnection downloadConnection = (HttpURLConnection) downloadUrl.openConnection();

        if (downloadConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {

            InputStream inputStream = downloadConnection.getInputStream();

            // opens an output stream to save into file
            FileOutputStream outputStream = new FileOutputStream("C:\\converted_file.doc");

            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }// w w  w  . j a v a  2s  .c o  m

            outputStream.close();
            inputStream.close();
        }

        downloadConnection.disconnect();
    }

From source file:de.flapdoodle.embed.process.io.file.Files.java

public static void write(InputStream in, File output) throws IOException {
    FileOutputStream out = new FileOutputStream(output);

    try {/*from   ww  w .ja va  2 s .  c o m*/
        byte[] buf = new byte[BYTE_BUFFER_LENGTH];
        int read;
        while ((read = in.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, read);
        }
    } finally {
        out.close();
    }
}

From source file:com.amazonaws.utilities.Util.java

/**
 * Copies the data from the passed in Uri, to a new file for use with the
 * Transfer Service/*from   ww w .j av  a 2s.c  o m*/
 * 
 * @param context
 * @param uri
 * @return
 * @throws IOException
 */
public static File copyContentUriToFile(Context context, Uri uri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(uri);
    File copiedData = new File(context.getDir("SampleImagesDir", Context.MODE_PRIVATE),
            UUID.randomUUID().toString());
    copiedData.createNewFile();

    FileOutputStream fos = new FileOutputStream(copiedData);
    byte[] buf = new byte[2046];
    int read = -1;
    while ((read = is.read(buf)) != -1) {
        fos.write(buf, 0, read);
    }

    fos.flush();
    fos.close();

    return copiedData;
}

From source file:fr.ece.epp.tools.Utils.java

public static void copy(File resFile, File objFolderFile) throws IOException {
    if (!resFile.exists()) {
        return;/*from   ww w .  j av  a  2  s  . c  o m*/
    }
    if (!objFolderFile.exists()) {
        objFolderFile.mkdirs();
    }
    if (resFile.isFile()) {
        File objFile = new File(objFolderFile.getPath() + File.separator + resFile.getName());
        InputStream ins = new FileInputStream(resFile);
        FileOutputStream outs = new FileOutputStream(objFile);
        byte[] buffer = new byte[1024 * 512];
        int length;
        while ((length = ins.read(buffer)) != -1) {
            outs.write(buffer, 0, length);
        }
        ins.close();
        outs.flush();
        outs.close();
    } else {
        String objFolder = objFolderFile.getPath() + File.separator + resFile.getName();
        File _objFolderFile = new File(objFolder);
        _objFolderFile.mkdirs();
        for (File sf : resFile.listFiles()) {
            copy(sf, new File(objFolder));
        }
    }
}