Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

In this page you can find the example usage for java.io FileOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:com.android.bandwidthtest.util.BandwidthTestUtil.java

/**
 * Download a given file from a target url to a given destination file.
 * @param targetUrl the url to download//from  w w w  .java2 s .  c  om
 * @param file the {@link File} location where to save to
 * @return true if it succeeded
 */
public static boolean DownloadFromUrl(String targetUrl, File file) {
    try {
        URL url = new URL(targetUrl);
        Log.d(LOG_TAG, "Download begining");
        Log.d(LOG_TAG, "Download url:" + url);
        Log.d(LOG_TAG, "Downloaded file name:" + file.getAbsolutePath());
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
    } catch (IOException e) {
        Log.d(LOG_TAG, "Failed to download file with error: " + e);
        return false;
    }
    return true;
}

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static void replaceAllInFile(File file, String oldValue, String newValue) throws IOException {
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);/*from  www  .ja  va 2s. co m*/
    is.close();
    String s = new String(b);
    s = s.replaceAll(oldValue, newValue);
    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();
}

From source file:cz.cuni.amis.planning4j.external.impl.itsimple.ItSimpleUtils.java

public static void extractFileIfNotExists(File targetFile, String resourcePath) {
    synchronized (fileOperationsMutex) {
        if (!targetFile.exists()) {
            InputStream inputStream = ItSimpleUtils.class.getResourceAsStream(resourcePath);
            if (inputStream == null) {
                throw new ItSimplePlanningException(
                        "Could not find file resource on classpath. Resource path:" + resourcePath);
            }//from  w  w  w .j  a  v  a  2s  . com
            if (!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs()) {
                throw new ItSimplePlanningException(
                        "Could not create parent dirs for file resource " + targetFile);
            }
            try {
                FileOutputStream outputfilestream = new FileOutputStream(targetFile);
                IOUtils.copy(inputStream, outputfilestream);
                outputfilestream.close();
            } catch (IOException ex) {
                throw new ItSimplePlanningException("Could not extract file resource to " + targetFile, ex);
            }
        }
    }
}

From source file:com.fjn.helper.common.io.file.zip.zip.ZipArchiveHelper.java

/**
 * zip/* ww  w .  ja v  a 2  s .c om*/
 *
 * @param desFile
 * @param filepathList
 */
public static void zip(File zipFile, List<String> filepathList, String filenameEncoding) {
    try {
        FileOutputStream out = new FileOutputStream(zipFile);
        zip(out, filepathList, filenameEncoding);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Copies source file to destination.//from   w w  w .ja  v a2  s.c o m
 * If destination file exists it will be overwritten silently.
 *
 * @param source file
 * @param target file
 */
public static void copyFile(final File source, final File target) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(target);
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static final void downloadBundle(InputStream is, String saveFilePath) throws IOException {
    checkDir(saveFilePath);/*from   www  .j  av a2 s  .  c  om*/
    File file = new File(saveFilePath, ZIP_BUNDLE_NAME);
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream outputStream = new FileOutputStream(file, false);

    byte[] buf = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) {
        outputStream.write(buf, 0, len);
    }
    outputStream.close();
    is.close();
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.log.ProblemsDatabaseUpdateJobTest.java

private static void createEmptyZip(File file) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(MINIMAL_ZIP_FILE, 0, 22);//w  ww  . j  av  a  2 s.  c  o m
    fos.flush();
    fos.close();
}

From source file:com.domnian.BotConfiguration.java

public static void writeDefault(File file) throws Exception {
    JSONObject defaultJson = new JSONObject();
    JSONObject connectDefault = new JSONObject();
    JSONObject identDefault = new JSONObject();
    JSONObject nickservDefault = new JSONObject();
    connectDefault.put("host", "irc.spi.gt");
    connectDefault.put("port", 6667);
    connectDefault.put("ssl", false);
    connectDefault.put("pass", "");
    connectDefault.put("channel", "#willies952002");
    defaultJson.put("connect", connectDefault);
    identDefault.put("user", "WilliesIRCBot");
    identDefault.put("nick", "WilliesIRCBot");
    identDefault.put("real", "Willies IRC Bot");
    defaultJson.put("ident", identDefault);
    nickservDefault.put("run", false);
    nickservDefault.put("email", "email@example.com");
    nickservDefault.put("password", "");
    defaultJson.put("nickserv", nickservDefault);
    defaultJson.put("manage", "admin");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(defaultJson.toString().getBytes("UTF-8"));
    fos.close();
}

From source file:Main.java

public static void copyToSD(InputStream in, String fileName) {
    String path = Environment.getExternalStorageDirectory() + File.separator + fileName;
    File file = new File(path);
    try {/*from  w w  w.j  a v a 2 s.  c o m*/
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getThumbnailImage(String path, int targetWidth) {
    Bitmap scaleImage = decodeScaleImage(path, targetWidth, targetWidth);
    try {//from   w  ww  . j  a v a 2 s .c  om
        File file = File.createTempFile("image", ".jpg");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        scaleImage.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
        fileOutputStream.close();
        return file.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
        return path;
    }
}