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:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static byte[] readBytes(File file) {
    FileInputStream in = null;
    final byte buffer[] = new byte[(int) file.length()];
    try {//from  ww  w .  ja v a 2 s  . com
        in = new FileInputStream(file);
        in.read(buffer);
    } catch (final IOException e) {
        MSGS.format(ERROR_READING_FILE_1, file.toString(), e);
    }
    if (in != null) {
        try {
            in.close();
        } catch (final IOException e) {
            // fail quietly
        }
    }
    return buffer;
}

From source file:Main.java

public static List<String> createListFromFile(File f) {
    List<String> retList = new ArrayList<String>();
    FileInputStream fis;
    BufferedReader br;/*w  w w. jav  a 2  s .c o m*/
    try {
        fis = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);

        String nStr;
        nStr = br.readLine();
        while (nStr != null) {
            retList.add(nStr);
            nStr = br.readLine();
        }

        br.close();
        fis.close();
    } catch (FileNotFoundException e) {
        logger.severe(e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        logger.severe(e.toString());
        e.printStackTrace();
    }

    return retList;
}

From source file:Main.java

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

    byte[] data;/* w  w w.  ja v a  2s .c om*/
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreFileUtil.java

/**
 * ?PFX?/*w  w  w . j  a va2s. c  o  m*/
 * 
 * @param alias ??
 * @param pfxPath PFX
 * @param password ?
 * @return
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws IOException
 * @throws UnrecoverableKeyException
 */
public static PrivateKey readPrivateKey(String alias, String pfxPath, String password) throws KeyStoreException,
        NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {

    KeyStore keyStore = KeyStore.getInstance("pkcs12");

    FileInputStream fis = null;

    fis = new FileInputStream(pfxPath);

    keyStore.load(fis, password.toCharArray());

    fis.close();

    return (PrivateKey) keyStore.getKey(alias, password.toCharArray());
}

From source file:Main.java

public static String readFile(File f) {
    try {//from w  w  w  .ja v  a  2s  .c o m
        FileInputStream fstream = new FileInputStream(f);
        InputStreamReader in = new InputStreamReader(fstream);
        BufferedReader br = new BufferedReader(in);
        String strLine;
        StringBuilder sb = new StringBuilder();
        while ((strLine = br.readLine()) != null) {
            sb.append(strLine);
            sb.append('\n');
        }
        br.close();
        in.close();
        fstream.close();
        return sb.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

public static BitmapDrawable getBitmapDrawableFromUrl(Resources res, URL trueUrl,
        BitmapFactory.Options mOptions) throws Exception {
    Bitmap bitmap = null;//from  w w w.  j  a v  a2  s  . c  o m
    FileInputStream mFS = null;
    try {
        mFS = new FileInputStream(trueUrl.getPath());
        bitmap = BitmapFactory.decodeFileDescriptor(mFS.getFD(), null, mOptions);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (mFS != null) {
            mFS.close();
        }
    }
    return new BitmapDrawable(res, bitmap);
}

From source file:fll.web.GatherBugReport.java

/**
 * Add the web application and tomcat logs to the zipfile.
 *///from  w  w w  .  j  a  va2s. co m
private static void addLogFiles(final ZipOutputStream zipOut, final ServletContext application)
        throws IOException {

    // get logs from the webapp
    final File fllAppDir = new File(application.getRealPath("/"));
    final File[] webLogs = fllAppDir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String name) {
            return name.startsWith("fllweb.log");
        }
    });
    if (null != webLogs) {
        for (final File f : webLogs) {
            if (f.isFile()) {
                FileInputStream fis = null;
                try {
                    zipOut.putNextEntry(new ZipEntry(f.getName()));
                    fis = new FileInputStream(f);
                    IOUtils.copy(fis, zipOut);
                    fis.close();
                } finally {
                    IOUtils.closeQuietly(fis);
                }
            }
        }
    }

    // get tomcat logs
    final File webappsDir = fllAppDir.getParentFile();
    LOGGER.trace("Webapps dir: " + webappsDir.getAbsolutePath());
    final File tomcatDir = webappsDir.getParentFile();
    LOGGER.trace("Tomcat dir: " + tomcatDir.getAbsolutePath());
    final File tomcatLogDir = new File(tomcatDir, "logs");
    LOGGER.trace("tomcat log dir: " + tomcatLogDir.getAbsolutePath());
    final File[] tomcatLogs = tomcatLogDir.listFiles();
    if (null != tomcatLogs) {
        for (final File f : tomcatLogs) {
            if (f.isFile()) {
                FileInputStream fis = null;
                try {
                    zipOut.putNextEntry(new ZipEntry(f.getName()));
                    fis = new FileInputStream(f);
                    IOUtils.copy(fis, zipOut);
                    fis.close();
                } finally {
                    IOUtils.closeQuietly(fis);
                }
            }
        }
    }

}

From source file:Main.java

public static String readXmlAsString(final File input) throws IOException {
    String xmlString = "";

    if (input == null) {
        throw new IOException("The input stream object is null.");
    }/*w ww  . j ava2s.com*/

    final FileInputStream fileInputStream = new FileInputStream(input);
    final InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line = bufferedReader.readLine();
    while (line != null) {
        xmlString += line + "\n";
        line = bufferedReader.readLine();
    }
    fileInputStream.close();
    fileInputStream.close();
    bufferedReader.close();

    return xmlString;
}

From source file:Main.java

static private void addToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, true);
    } else {/* ww w  .j av a  2 s .c  om*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = null;
        try {
            in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

From source file:edu.usf.cutr.siri.SiriParserJacksonExample.java

/**
 * Read in input file to string/*from ww  w. j a  va  2  s.c  om*/
 * @param file file containing the JSON or XML data
 * @return String representation of the JSON or XML file
 * @throws IOException
 */
private static String readFile(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}