List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:MappedChannelRead.java
public static void main(String args[]) { FileInputStream fIn; FileChannel fChan;//from w w w. j a v a 2 s . c om long fSize; MappedByteBuffer mBuf; try { fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File fromFile = new File("fromFile.txt"); File toFile = new File("toFile.txt"); FileInputStream inFile = new FileInputStream(fromFile); FileOutputStream outFile = new FileOutputStream(toFile); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); int bytesWritten = 0; long byteCount = inChannel.size(); while (bytesWritten < byteCount) { bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel); }/*ww w .j a va2 s . c o m*/ inFile.close(); outFile.close(); }
From source file:Main.java
public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); }/*from ww w . ja v a2 s .c om*/ System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); }
From source file:RealFunctionValidation.java
public static void main(final String[] args) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (args.length == 0) { final String msg = "missing required properties file"; throw new IllegalArgumentException(msg); }/*from w ww.j av a 2s . c o m*/ final FileInputStream in = new FileInputStream(args[0]); final Properties properties = new Properties(); properties.load(in); in.close(); final ApplicationProperties p; p = ApplicationProperties.create(properties); run(p); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileInputStream inStream = new FileInputStream("test.txt"); ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream("compressed.zip")); outStream.putNextEntry(new ZipEntry("test.txt")); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, bytesRead); }/*from w w w .j ava2 s . co m*/ outStream.closeEntry(); outStream.close(); inStream.close(); }
From source file:Main.java
public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); }//from ww w . j a v a2 s . co m System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); }
From source file:com.renren.ntc.sg.util.wxpay.https.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream( new File("/Users/allenz/Downloads/wx_cert/apiclient_cert.p12")); try {/*from ww w.ja v a 2s . c o m*/ keyStore.load(instream, Constants.mch_id.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, Constants.mch_id.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 post = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"); System.out.println("executing request" + post.getRequestLine()); String openid = "oQfDLjmZD7Lgynv6vuoBlWXUY_ic"; String nonce_str = Sha1Util.getNonceStr(); String orderId = SUtils.getOrderId(); String re_user_name = "?"; String amount = "1"; String desc = ""; String spbill_create_ip = "123.56.102.224"; String txt = TXT.replace("{mch_appid}", Constants.mch_appid); txt = txt.replace("{mchid}", Constants.mch_id); txt = txt.replace("{openid}", openid); txt = txt.replace("{nonce_str}", nonce_str); txt = txt.replace("{partner_trade_no}", orderId); txt = txt.replace("{check_name}", "FORCE_CHECK"); txt = txt.replace("{re_user_name}", re_user_name); txt = txt.replace("{amount}", amount); txt = txt.replace("{desc}", desc); txt = txt.replace("{spbill_create_ip}", spbill_create_ip); SortedMap<String, String> map = new TreeMap<String, String>(); map.put("mch_appid", Constants.mch_appid); map.put("mchid", Constants.mch_id); map.put("openid", openid); map.put("nonce_str", nonce_str); map.put("partner_trade_no", orderId); //FORCE_CHECK| OPTION_CHECK | NO_CHECK map.put("check_name", "OPTION_CHECK"); map.put("re_user_name", re_user_name); map.put("amount", amount); map.put("desc", desc); map.put("spbill_create_ip", spbill_create_ip); String sign = SUtils.createSign(map).toUpperCase(); txt = txt.replace("{sign}", sign); post.setEntity(new StringEntity(txt, "utf-8")); CloseableHttpResponse response = httpclient.execute(post); 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) { sb.append(text); } String resp = sb.toString(); LoggerUtils.getInstance().log(String.format("req %s rec %s", txt, resp)); if (isOk(resp)) { String payment_no = getValue(resp, "payment_no"); LoggerUtils.getInstance() .log(String.format("order %s pay OK payment_no %s", orderId, payment_no)); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:Main.java
public static void main(String[] args) throws IOException { Deflater def = new Deflater(); byte[] input = new byte[1024]; byte[] output = new byte[1024]; FileInputStream fin = new FileInputStream("a.dat"); FileOutputStream fout = new FileOutputStream("b.dat"); int numRead = fin.read(input); def.setInput(input, 0, numRead);/*from w ww. ja v a 2 s . c o m*/ while (!def.needsInput()) { int numCompressedBytes = def.deflate(output, 0, output.length); if (numCompressedBytes > 0) { fout.write(output, 0, numCompressedBytes); } } def.finish(); fin.close(); fout.flush(); fout.close(); def.reset(); }
From source file:ExplicitChannelRead.java
public static void main(String args[]) { FileInputStream fIn; FileChannel fChan;/* w w w.ja va2s. c om*/ long fSize; ByteBuffer mBuf; try { fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf); mBuf.rewind(); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:net.itransformers.idiscover.discoverylisteners.XmlTopologyDeviceLogger.java
public static void main(String[] args) throws IOException, JAXBException { String path = "devices_and_models\\lab\\undirected"; File dir = new File(path); String[] files = dir.list(new FilenameFilter() { public boolean accept(File dir, String name) { return (name.endsWith(".hml")); }//from w w w .j a v a2 s . c o m }); Map<String, String> params = new HashMap<String, String>(); params.put("path", path); params.put("conf/xslt", "iDiscover\\conf\\xslt\\transformator3.xslt"); XmlTopologyDeviceLogger logger = new XmlTopologyDeviceLogger(params, null, null); for (String fileName : files) { FileInputStream is = new FileInputStream(path + File.separator + fileName); DiscoveredDeviceData discoveredDeviceData; try { discoveredDeviceData = JaxbMarshalar.unmarshal(DiscoveredDeviceData.class, is); } finally { is.close(); } String deviceName = fileName.substring("device-".length(), fileName.length() - ".xml".length()); logger.handleDevice(deviceName, null, discoveredDeviceData, null); } }