List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:Main.java
public static Object readObject(Context context, String filename) { FileInputStream in = null; ObjectInputStream oin = null; Object data = null;/*from w w w . ja va 2 s . c om*/ try { in = context.openFileInput(filename + ".odb"); oin = new ObjectInputStream(in); data = oin.readObject(); oin.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } return data; }
From source file:Main.java
public static String readFileAsString(File file) throws IOException { FileInputStream fin = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); StringBuilder sb = new StringBuilder(); String line = null;//from ww w. j ava 2 s . c o m while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } fin.close(); return sb.toString(); }
From source file:Main.java
public static Bitmap getBitmapByFile(File file) { FileInputStream fis = null; Bitmap bitmap = null;//from ww w .ja v a 2 s. c o m try { fis = new FileInputStream(file); bitmap = BitmapFactory.decodeStream(fis); } catch (FileNotFoundException e) { } catch (OutOfMemoryError e) { } finally { try { fis.close(); } catch (Exception e) { } } return bitmap; }
From source file:Main.java
public static byte[] getByteArray(File filePath, String name) { FileInputStream fileInputStream = null; File file = new File(filePath, name); byte[] bFile = new byte[(int) file.length()]; try {/*from www. ja va 2 s . co m*/ //convert file into array of bytes fileInputStream = new FileInputStream(file); fileInputStream.read(bFile); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } return bFile; }
From source file:Main.java
public static void CopyFile(String source, String destination) throws IOException { FileInputStream fin = new FileInputStream(source); FileOutputStream fout = new FileOutputStream(destination); byte[] b = new byte[1024 * 1024]; int noOfBytes = 0; while ((noOfBytes = fin.read(b)) != -1) { fout.write(b, 0, noOfBytes);/*w w w.jav a 2 s . c o m*/ } fin.close(); fout.close(); }
From source file:Main.java
public static ByteBuffer mapFile(File f, long offset, ByteOrder byteOrder) throws IOException { FileInputStream dataFile = new FileInputStream(f); try {//from w w w. j a va 2s. c om FileChannel fc = dataFile.getChannel(); MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, f.length() - offset); buffer.order(byteOrder); return buffer; } finally { dataFile.close(); // this *also* closes the associated channel, fc } }
From source file:com.bbc.util.ClientCustomSSL.java
public static String clientCustomSLL(String mchid, String path, String data) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); System.out.println("?..."); FileInputStream instream = new FileInputStream(new File("/payment/apiclient_cert.p12")); try {/*from w w w . j a v a 2 s. c om*/ keyStore.load(instream, mchid.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchid.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(path); 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(); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; StringBuffer sb = new StringBuffer(""); while ((text = bufferedReader.readLine()) != null) { System.out.println(text); sb.append(text); } return sb.toString(); } EntityUtils.consume(entity); return ""; } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.openkm.util.ArchiveUtils.java
/** * Create ZIP archive from file//from w w w . ja v a 2s . c om */ public static void createZip(File path, OutputStream os) throws IOException { log.debug("createZip({}, {})", new Object[] { path, os }); if (path.exists() && path.canRead()) { ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os); zaos.setComment("Generated by OpenKM"); zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS); zaos.setUseLanguageEncodingFlag(true); zaos.setFallbackToUTF8(true); zaos.setEncoding("UTF-8"); log.debug("FILE {}", path); ZipArchiveEntry zae = new ZipArchiveEntry(path.getName()); zaos.putArchiveEntry(zae); FileInputStream fis = new FileInputStream(path); IOUtils.copy(fis, zaos); fis.close(); zaos.closeArchiveEntry(); zaos.flush(); zaos.finish(); zaos.close(); } else { throw new IOException("Can't access " + path); } log.debug("createZip: void"); }
From source file:Main.java
public static String compress(String filename) throws FileNotFoundException, IOException { byte[] buffer = new byte[4096]; int bytesRead; String[] entries = { ".mp4" }; String zipfile = filename.replace(".mp4", ".zip"); if (!(new File(zipfile)).exists()) { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); out.setLevel(Deflater.BEST_COMPRESSION); out.setMethod(ZipOutputStream.DEFLATED); for (int i = 0; i < entries.length; i++) { File f = new File(filename.replace(".mp4", entries[i])); if (f.exists()) { FileInputStream in = new FileInputStream(f); ZipEntry entry = new ZipEntry(f.getName()); out.putNextEntry(entry); while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); }/* ww w . j av a 2 s . co m*/ } out.close(); } return zipfile; }
From source file:Main.java
public static void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.flush();/*from w w w . jav a2 s .c om*/ outStream.close(); }