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

public static String getFileContent(File file) {
    FileInputStream fis = null;
    try {/*  ww  w  .ja  v a2s  .c o m*/
        fis = new FileInputStream(file);
        return getInputStreamString(fis);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:ch.entwine.weblounge.common.impl.util.TestUtils.java

/**
 * Loads the <code>JSON</code> data from the specified file on the class path
 * and returns it after having stripped off any newlines, line breaks and
 * otherwise disturbing spaces and characters.
 * /*  www.  ja va  2s.c o  m*/
 * @param path
 *          the resource path
 * @return the contents of the resource
 */
public static String loadJsonFromResource(String path) {
    File templateFile = new File(TestUtils.class.getResource(path).getPath());
    String template = null;
    try {
        byte[] buffer = new byte[(int) templateFile.length()];
        FileInputStream f = new FileInputStream(templateFile);
        f.read(buffer);
        f.close();
        template = new String(buffer, "utf-8");
        template = template.replaceAll("(\"\\s*)", "\"").replaceAll("(\\s*\")+", "\"");
        template = template.replaceAll("(\\s*\\{\\s*)", "{").replaceAll("(\\s*\\}\\s*)", "}");
        template = template.replaceAll("(\\s*\\[\\s*)", "[").replaceAll("(\\s*\\]\\s*)", "]");
        template = template.replaceAll("(\\s*,\\s*)", ",");
    } catch (IOException e) {
        throw new RuntimeException("Error reading test resource at " + path);
    }
    return template;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String fileName) {
    FileInputStream fis = null;
    Bitmap bitmap = null;// w  ww  . j  a  va 2  s  .c o  m
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
    } catch (OutOfMemoryError e) {
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:hudson.plugins.clearcase.ClearCaseChangeLogSet.java

/**
 * Parses the change log file and returns a ClearCase change log set.
 * /*  w  ww  . j  a  v a2  s. co  m*/
 * @param build the build for the change log
 * @param changeLogFile the change log file
 * @return the change log set
 */
public static ClearCaseChangeLogSet parse(AbstractBuild<?, ?> build, File changeLogFile)
        throws IOException, SAXException {
    FileInputStream fileInputStream = new FileInputStream(changeLogFile);
    ClearCaseChangeLogSet logSet = parse(build, fileInputStream);
    fileInputStream.close();
    return logSet;
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Recursive method for adding contents (also sub folders) of a folder to a Zip file.
 * Code comes from://  w  w w .  j a  va 2  s  .  c o  m
 * <tt>http://www.java2s.com/Code/Java/File-Input-Output/
 * Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm</tt>.
 * @param basePath The base path of the Zip folder (for creating relative folders inside the Zip archive).
 * @param directory The current directory which shall be zipped into the Zip archive.
 * @param out A Zip archive.
 * @throws IOException if an I/O error has occurred
 */
private static void addDir(String basePath, File directory, ZipOutputStream out) throws IOException {
    File[] files = directory.listFiles();

    for (int i = 0; i < files.length; i++) {
        String entryName = makeRelative(basePath, files[i]);
        if (null != entryName && !entryName.isEmpty()) {
            if (files[i].isDirectory()) {
                out.putNextEntry(new ZipEntry(entryName));
                out.closeEntry();
                addDir(basePath, files[i], out);
            } else {
                out.putNextEntry(new ZipEntry(entryName));
                FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
                try {
                    IOUtils.copy(in, out);
                    in.close();
                } catch (IOException e1) {
                    IOUtils.closeQuietly(in);
                    throw e1;
                }
                out.closeEntry();
            }
        }
    }
}

From source file:Main.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, false);
    } else {//  www . j  a v  a 2  s .c  o  m
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        try {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}

From source file:Main.java

public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
        return;//from   ww w .j  a v a 2s. c  o  m
    }

    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }

    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        FileInputStream fosfrom = new FileInputStream(fromFile);
        FileOutputStream fosto = new FileOutputStream(toFile);

        byte[] bt = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void compressFiles(File files[], File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    for (int i = 0; i < files.length; i++) {
        FileInputStream fileInputStream = new FileInputStream(files[i]);
        zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath()));

        int size;
        while ((size = fileInputStream.read(buffer)) > 0)
            zipOutputStream.write(buffer, 0, size);

        zipOutputStream.closeEntry();//from   ww w  . j  a v  a 2 s  . c  om
        fileInputStream.close();
    }

    zipOutputStream.close();
}

From source file:Main.java

public static Bitmap safeDecodeBimtapFile(String bmpFile, BitmapFactory.Options opts) {

    BitmapFactory.Options optsTmp = opts;
    if (optsTmp == null) {
        optsTmp = new BitmapFactory.Options();
        optsTmp.inSampleSize = 1;//w  ww  .  j  av a2  s .  co  m
    }

    Bitmap bmp = null;
    FileInputStream input = null;

    final int MAX_TRIAL = 5;
    for (int i = 0; i < MAX_TRIAL; ++i) {
        try {
            input = new FileInputStream(bmpFile);
            bmp = BitmapFactory.decodeStream(input, null, opts);
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            optsTmp.inSampleSize *= 2;
            try {
                input.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            break;
        }
    }

    return bmp;
}

From source file:com.deying.util.weixin.ClientCustomSSL.java

public static String doRefund(String url, String data) throws Exception {
    System.out.println("111111111111111111111111111111111111111111111118 ");
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(new File("E:/apiclient_cert.p12"));//P12
    try {//from w ww  .j  a  v a2  s  . com
        keyStore.load(instream, "1233374102".toCharArray());//?..MCHID
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1233374102".toCharArray())//?
            .build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {
        HttpPost httpost = new HttpPost(url); // ??
        httpost.addHeader("Connection", "keep-alive");
        httpost.addHeader("Accept", "*/*");
        httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpost.addHeader("Host", "api.mch.weixin.qq.com");
        httpost.addHeader("X-Requested-With", "XMLHttpRequest");
        httpost.addHeader("Cache-Control", "max-age=0");
        httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        httpost.setEntity(new StringEntity(data, "UTF-8"));
        CloseableHttpResponse response = httpclient.execute(httpost);
        try {
            HttpEntity entity = response.getEntity();

            String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            EntityUtils.consume(entity);
            return jsonStr;
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}