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.ecoefficiencytool.core.tools.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info(/*from  ww w  .ja v  a 2s .  c  om*/
            "ECO: File " + fileObject.getAbsolutePath() + " didn't exist. Creating one with default values...");

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

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

    //Copy file.
    log.info("ECO: 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 ignore) {
        log.error("ECO: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("ECO: Couldn't close file output stream");
    }
}

From source file:eu.optimis.mi.aggregator.util.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws IOException {
    log.debug("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.//from  w ww  .ja  v  a 2  s. com
    log.debug("Copying file " + fileObject.getName() + "into path: " + fileObject.getAbsolutePath());
    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());
        throw new IOException();
    }
    try {
        streamOut.close();
    } catch (IOException ex) {
        log.error("Couldn't close file output stream");
        log.error(ex.getMessage());
        throw new IOException();
    }
}

From source file:Main.java

static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException {
    URL url = new URL(downloadUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);//w  w w  . j a  va  2s  . c om
    urlConnection.connect();
    int code = urlConnection.getResponseCode();
    if (code > 300 || code == -1) {
        throw new IOException("Cannot read url: " + downloadUrl);
    }
    String filePath = prepareFilePath(fileName, dir);
    String tmpFilePath = prepareTmpFilePath(fileName, dir);
    FileOutputStream fileOutput = new FileOutputStream(tmpFilePath);
    BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    byte[] buffer = new byte[1024];
    int bufferLength;
    while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
    }
    fileOutput.close();
    // move tmp to destination
    copyFile(new File(tmpFilePath), new File(filePath));
    return filePath;
}

From source file:Main.java

public static boolean copyFile(Context context, String assetPath, String sdcardPath) {
    InputStream is = null;/* w w w  . j  a va2 s .c o m*/
    FileOutputStream fs = null;

    boolean result = false;
    try {
        int byteread = 0;
        is = context.getAssets().open(assetPath);
        fs = new FileOutputStream(sdcardPath);
        byte[] buffer = new byte[1024];
        while ((byteread = is.read(buffer)) != -1) {
            fs.write(buffer, 0, byteread);
        }

        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
            if (fs != null)
                fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:Main.java

public static void gunzip(File inFile, File outFile) {
    System.out.println("Expanding " + inFile.getAbsolutePath() + " to " + outFile.getAbsolutePath());

    FileOutputStream out = null;
    GZIPInputStream zIn = null;/*from w w w  .  j  a v a 2s . c om*/
    FileInputStream fis = null;
    try {
        out = new FileOutputStream(outFile);
        fis = new FileInputStream(inFile);
        zIn = new GZIPInputStream(fis);
        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = zIn.read(buffer, 0, buffer.length);
        } while (count != -1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static void copyRAWFile(InputStream inStream, FileOutputStream outStream) {
    try {/*from  ww w  .j a v a2 s.c  o m*/
        int bytesum = 0, byteread = 0;

        byte[] buffer = new byte[102400]; //100k buffer
        while ((byteread = inStream.read(buffer)) != -1) {
            bytesum += byteread;
            System.out.println(bytesum);
            outStream.write(buffer, 0, byteread);
        }
        inStream.close();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:eu.optimis.infrastructureproviderriskassessmenttool.core.configration.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info("IPRA: File " + fileObject.getAbsolutePath()
            + " didn't exist. Creating one with default values...");

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

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

    //Copy file./*from  w w w.  j  a  va2s .  c  o m*/
    log.info("IPRA: 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 ignore) {
        log.error("IPRA: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("IPRA: Couldn't close file output stream");
    }
}

From source file:eu.optimis.serviceproviderriskassessmenttool.core.configration.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.info("SPRA: File " + fileObject.getAbsolutePath()
            + " didn't exist. Creating one with default values...");

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

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

    //Copy file./*from  w  w w. java 2 s  . co m*/
    log.info("SPRA: 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 ignore) {
        log.error("SPRA: Couldn't close input stream");
    }
    try {
        streamOut.close();
    } catch (IOException ignore) {
        log.error("SPRA: Couldn't close file output stream");
    }
}

From source file:Main.java

public static boolean copyFile(File src, File dest) {
    if (!src.exists())
        return false;

    FileInputStream fis = null;//from ww  w .  j  a  v  a2s . co m
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(src);
        fos = new FileOutputStream(dest);

        byte[] buffer = new byte[2048];
        int bytesread = 0;
        while ((bytesread = fis.read(buffer)) != -1) {
            if (bytesread > 0)
                fos.write(buffer, 0, bytesread);
        }

        return true;

    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e2) {
            }
        }

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

From source file:eu.optimis.ip.gui.client.resources.ConfigManager.java

private static void createDefaultConfigFile(File fileObject) throws Exception {
    log.debug("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./*from   ww  w .j  a  v  a2  s  .c o m*/
    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());
    }
}