List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:com.hilatest.httpclient.apacheexample.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("my.keystore")); try {//w w w .ja va 2 s.c o m trustStore.load(instream, "nopassword".toCharArray()); } finally { instream.close(); } SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpget = new HttpGet("https://localhost/"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:Main.java
public static void main(String[] args) throws Exception { OutputStream os = new FileOutputStream("C://test.txt"); FilterOutputStream fos = new FilterOutputStream(os); fos.write(65);/*from ww w . j a v a 2 s. c o m*/ // forces byte contents to written out to the stream fos.flush(); // create output streams FileInputStream fis = new FileInputStream("C://test.txt"); // read byte int i = fis.read(); // convert integer to characters char c = (char) i; System.out.print("Character read: " + c); fos.close(); fis.close(); }
From source file:com.lxf.spider.client.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("my.keystore")); try {/* www .ja v a2s . c o m*/ trustStore.load(instream, "nopassword".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .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 { HttpGet httpget = new HttpGet("https://localhost/"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:test.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("D:\\keystore.jks")); try {// www .ja v a 2s . c o m trustStore.load(instream, "password".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .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 { HttpGet httpget = new HttpGet("https://retail.onlinesbi.com/personal/css/style.css"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String[] filenames = new String[] { "filename1", "filename2" }; byte[] buf = new byte[1024]; String outFilename = "outfile.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); for (int i = 0; i < filenames.length; i++) { FileInputStream in = new FileInputStream(filenames[i]); out.putNextEntry(new ZipEntry(filenames[i])); int len;/*w ww . j a v a 2s . c o m*/ while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "from java2s.com!"; try {// ww w .jav a 2 s .co m OutputStream os = new FileOutputStream("test.txt"); OutputStreamWriter writer = new OutputStreamWriter(os); FileInputStream in = new FileInputStream("test.txt"); writer.write(s, 0, 5); writer.flush(); for (int i = 0; i < 5; i++) { System.out.print((char) in.read()); } writer.close(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { String s = "from java2s.com!"; try {//from www .j a v a2 s . c om OutputStream os = new FileOutputStream("test.txt"); OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset().newEncoder()); FileInputStream in = new FileInputStream("test.txt"); writer.write(s, 0, 5); writer.flush(); for (int i = 0; i < 5; i++) { System.out.print((char) in.read()); } writer.close(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { String s = "from java2s.com!"; try {/*from w ww .j av a2 s. co m*/ OutputStream os = new FileOutputStream("test.txt"); OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset()); FileInputStream in = new FileInputStream("test.txt"); writer.write(s, 0, 5); writer.flush(); for (int i = 0; i < 5; i++) { System.out.print((char) in.read()); } writer.close(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { OutputStream os = new FileOutputStream("C://test.txt"); FilterOutputStream fos = new FilterOutputStream(os); // writes buffer to the output stream fos.write(65);/*from w ww .j a v a 2 s .c o m*/ // forces byte contents to written out to the stream fos.flush(); // create input streams FileInputStream fis = new FileInputStream("C://test.txt"); // get byte from the file int i = fis.read(); // convert integer to character char c = (char) i; System.out.print("Character read: " + c); fos.close(); fis.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "from java2s.com!"; try {/*from w w w . j av a 2s.c o m*/ OutputStream os = new FileOutputStream("test.txt"); OutputStreamWriter writer = new OutputStreamWriter(os, java.nio.charset.StandardCharsets.UTF_8); FileInputStream in = new FileInputStream("test.txt"); writer.write(s, 0, 5); writer.flush(); for (int i = 0; i < 5; i++) { System.out.print((char) in.read()); } writer.close(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } }