Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

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

Usage

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

public static void replaceAllInFile(File file, String[] oldValues, String[] newValues) throws IOException {
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);//  w  w w.j  av a 2 s .  co m
    is.close();
    String s = new String(b);

    for (int i = 0; i < oldValues.length; i++) {
        s = s.replaceAll(oldValues[i], newValues[i]);
    }

    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();

}

From source file:com.nineteendrops.tracdrops.client.core.Utils.java

public static byte[] getBinaryData(String fullFileName, TracProperties tracProperties) {

    File file = new File(fullFileName);
    if (!file.exists()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }/*from w  w  w  .ja va2  s .  co  m*/

    if (!file.canRead()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }

    if (!file.isFile()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }

    int maxUploadSize = tracProperties.getIntegerProperty(TracProperties.ATTACHMENT_UPLOAD_MAX_SIZE);
    if (file.length() > maxUploadSize) {
        throw new TracException(MessageUtils.getMessage("core.file.size.exceeded", fullFileName,
                String.valueOf(maxUploadSize)));
    }

    byte[] binaryData;
    try {
        binaryData = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(binaryData);
        fis.close();
    } catch (IOException e) {
        throw new TracException(MessageUtils.registerErrorLog(log, "core.unexpected.exception", e.getMessage()),
                e);
    }
    return binaryData;
}

From source file:Main.java

/**
 * Copy a file from one place to another
 *//*from   w  w  w  . j  av  a2  s  . c  om*/
private static void copyFile(File in, File out) throws Exception {
    FileInputStream fis = new FileInputStream(in);
    FileOutputStream fos = new FileOutputStream(out);
    try {
        copyStream(fis, fos);
    } finally {
        fis.close();
        fos.close();
    }
}

From source file:com.technofovea.packbsp.PackbspUtil.java

/**
 * Efficiently copies a given file, returning a temporary copy which will be
 * removed when execution finishes.//from   w  w  w . j av  a  2 s  .  c om
 * 
 * @param source Source file to copy.
 * @return The copied file.
 * @throws IOException
 */
public static final File createTempCopy(File source) throws IOException {
    String ext = FilenameUtils.getExtension(source.getName());
    File dest = File.createTempFile("packbsp_temp_", "." + ext);
    dest.deleteOnExit();
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(dest);
    IOUtils.copy(fis, fos);
    fis.close();
    fos.close();
    return dest;
}

From source file:Main.java

/**
 * Copy a file from one place to another
 * @throws Exception/* w  w  w.  jav a  2 s  . co  m*/
 */
public static void copyFile(File in, File out) throws Exception {
    FileInputStream fis = new FileInputStream(in);
    FileOutputStream fos = new FileOutputStream(out);
    try {
        copyStream(fis, fos);
    } finally {
        fis.close();
        fos.close();
    }
}

From source file:Main.java

public static byte[] read(Context context, String fileName) throws FileNotFoundException, IOException {

    File file = getFile(context, fileName);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    if (file != null) {
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);// w w  w  .j  a  va 2 s  . c o m
        bos.write(data);
        fis.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

public static void pokeValue(final File file, final String xpathExpression, final String value)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException,
        TransformerException {/*from  ww  w .jav a2 s . c  om*/
    // 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:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

public static Bitmap getImage(Context context, String fileName) throws IOException {
    FileInputStream fis = getFileInputStream(context, fileName);
    Bitmap bm = BitmapFactory.decodeStream(fis);
    fis.close();

    return bm;//w w w .  j  a v  a  2 s.  c o m
}

From source file:Main.java

public static Object readAsObject(File source) throws IOException, ClassNotFoundException {
    FileInputStream in = new FileInputStream(source);
    try {/* w  w w.j a v  a  2 s. c o  m*/
        ObjectInputStream oi = new ObjectInputStream(in);
        return oi.readObject();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean isCACertificateInstalled(File fileCA, String type, char[] password)
        throws KeyStoreException {

    KeyStore keyStoreCA = null;// w ww  . ja va  2 s.c  o m
    try {
        keyStoreCA = KeyStore.getInstance(type/*, "BC"*/);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (fileCA.exists() && fileCA.canRead()) {
        try {
            FileInputStream fileCert = new FileInputStream(fileCA);
            keyStoreCA.load(fileCert, password);
            fileCert.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (java.security.cert.CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration ex = keyStoreCA.aliases();
        Date exportFilename = null;
        String caAliasValue = "";

        while (ex.hasMoreElements()) {
            String is = (String) ex.nextElement();
            Date lastStoredDate = keyStoreCA.getCreationDate(is);
            if (exportFilename == null || lastStoredDate.after(exportFilename)) {
                exportFilename = lastStoredDate;
                caAliasValue = is;
            }
        }

        try {
            return keyStoreCA.getKey(caAliasValue, password) != null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        }
    }
    return false;
}