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

public static void pokeValue(final File file, final String xpathExpression, final String value)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException,
        TransformerException {/*  www. j  ava 2 s .c  o  m*/
    // TODO: it might be better performance to do a SAX read/write
    final DocumentBuilder db = DBF.newDocumentBuilder();
    final FileInputStream fis = new FileInputStream(file);
    final Document doc;
    try {
        doc = db.parse(fis);
    } finally {
        fis.close();
    }

    pokeValue(doc, xpathExpression, value);

    final Transformer t = TF.newTransformer();
    final DOMSource source = new DOMSource(doc);
    final FileOutputStream fos = new FileOutputStream(file);
    try {
        final StreamResult result = new StreamResult(fos);
        t.transform(source, result);
    } finally {
        fos.close();
    }
}

From source file:MainServer.UploadServlet.java

public static void inputStream2File(InputStream is, String savePath) throws Exception {
    System.out.println("Save path:" + savePath);
    File file = new File(savePath);
    InputStream inputStream = is;
    BufferedInputStream fis = new BufferedInputStream(inputStream);
    FileOutputStream fos = new FileOutputStream(file);
    int f;//from  w ww . ja  v  a  2 s  .c  o  m
    while ((f = fis.read()) != -1) {
        fos.write(f);
    }
    fos.flush();
    fos.close();
    fis.close();
    inputStream.close();
}

From source file:Main.java

/**
 * Copy a file from one place to another
 *///  w  w w . ja  v a 2 s  .  c o m
private static void copyFile(File in, File out) throws Exception {
    FileInputStream fis = new FileInputStream(in);
    FileOutputStream fos = new FileOutputStream(out);
    try {
        copyStream(fis, fos);
    } catch (Exception e) {
        throw e;
    } finally {
        fis.close();
        fos.close();
    }
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts the byte[] to a file./*  ww w.  j  a v  a  2s . c  o  m*/
 *
 * @param path The destination path.
 * @param buf  The buffer.
 * @param key  The key.
 */
public static void encryptToFile(String path, byte[] buf, String key) {
    try {
        FileOutputStream fos = new FileOutputStream(path);
        fos.write(encrypt(buf, key).getBytes());
        fos.close();
    } catch (Exception e) {
        logger.warn("Could not encrypt to file {}", path, e);
    }
}

From source file:Main.java

public static String saveToInternalSorage(Bitmap bitmapImage, Context context, int ID, String directoryName) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir(directoryName, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_data/imageDir
    File path = new File(directory, "mpp_profile_pic_" + Integer.toString(ID)); // Create imageDir

    FileOutputStream fos = null;

    try {/*from   w w  w  . j a  v a  2  s.  c  o  m*/
        fos = new FileOutputStream(path);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); // Use the compress method on the BitMap object to write image to the OutputStream
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return directory.getAbsolutePath();
}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {/*from   ww w  . ja v a  2  s.c o  m*/
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static long write(String fileName, String data, int position) throws FileNotFoundException, IOException {
    boolean append = false;
    if (position > 0) {
        truncateFile(fileName, position);
        append = true;//from w  ww.jav a2s . c om
    }

    byte[] rawData = data.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    FileOutputStream out = new FileOutputStream(fileName, append);
    byte buff[] = new byte[rawData.length];
    in.read(buff, 0, buff.length);
    out.write(buff, 0, rawData.length);
    out.flush();
    out.close();

    return data.length();
}

From source file:Main.java

/**
 * Uncompress gzipped files//from   ww  w. j a v a 2  s . c  om
 * @param gzippedFile The file to uncompress
 * @param destinationFile The resulting file
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static void gunzip(File gzippedFile, File destinationFile) throws IOException {
    int buffer = 2048;

    FileInputStream in = new FileInputStream(gzippedFile);
    GZIPInputStream zipin = new GZIPInputStream(in);

    byte[] data = new byte[buffer];

    // decompress the file
    FileOutputStream out = new FileOutputStream(destinationFile);
    try {
        int length;
        while ((length = zipin.read(data, 0, buffer)) != -1)
            out.write(data, 0, length);
    } finally {
        out.close();

        zipin.close();
        in.close();
    }

}

From source file:Main.java

public static void copyFile(File fromFile, File toFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(fromFile);
    FileOutputStream outputStream = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    for (int numRead; (numRead = inputStream.read(buffer)) != -1;)
        outputStream.write(buffer, 0, numRead);
    inputStream.close();//from   w w w . j a v a2  s . c o  m
    outputStream.close();
}

From source file:Main.java

/**
 * Creates a copy of the file, ensuring the file is written to the disk
 * @param in Source file/*  w w  w  . j  a v a 2  s . c  o  m*/
 * @param out Destination file
 * @throws IOException if the operation fails
 */
public static void copyFileSync(File in, File out) throws IOException {
    FileInputStream inStream = new FileInputStream(in);
    FileOutputStream outStream = new FileOutputStream(out);
    try {
        copyStream(inStream, outStream);
    } finally {
        inStream.close();
        outStream.flush();
        outStream.getFD().sync();
        outStream.close();
    }
}