List of usage examples for java.io InputStreamReader InputStreamReader
public InputStreamReader(InputStream in)
From source file:MainClass.java
public static void main(String[] args) throws Exception { URL u = new URL("http://www.java2s.com"); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); System.out.println("HTTP/1.x " + code + " " + response); for (int j = 1;; j++) { String header = uc.getHeaderField(j); String key = uc.getHeaderFieldKey(j); if (header == null || key == null) break; System.out.println(uc.getHeaderFieldKey(j) + ": " + header); }//from w w w . j a v a 2s . co m InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { char[] passphrase = "password".toCharArray(); KeyStore keystore = KeyStore.getInstance("JKS"); keystore.load(new FileInputStream(".keystore"), passphrase); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(keystore, passphrase);// w w w.j ava2 s . c o m SSLContext context = SSLContext.getInstance("TLS"); KeyManager[] keyManagers = kmf.getKeyManagers(); context.init(keyManagers, null, null); SSLServerSocketFactory ssf = context.getServerSocketFactory(); ServerSocket ss = ssf.createServerSocket(PORT); Socket s = ss.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while (((line = in.readLine()) != null)) { System.out.println(line); } in.close(); s.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];//from w ww . jav a 2 s .c o m URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoInput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:Main.java
public static void main(String args[]) throws Exception { String fullURL = args[0];//from w ww . j a v a 2 s .c o m URL u = new URL(fullURL); URLConnection conn = u.openConnection(); conn.setDoOutput(true); OutputStream theControl = conn.getOutputStream(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl)); for (int i = 1; i < args.length; i++) { out.write(args[i] + "\n"); } out.close(); InputStream theData = conn.getInputStream(); String contentType = conn.getContentType(); if (contentType.toLowerCase().startsWith("text")) { BufferedReader in = new BufferedReader(new InputStreamReader(theData)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } }
From source file:Redirecting.java
public static void main(String[] args) throws IOException { PrintStream console = System.out; BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java")); PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out"))); System.setIn(in);/*from w ww . ja va 2s . co m*/ System.setOut(out); System.setErr(out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) System.out.println(s); out.close(); // Remember this! System.setOut(console); }
From source file:Who.java
public static void main(String[] v) { Socket s = null;/* w w w . jav a 2 s . com*/ PrintWriter out = null; BufferedReader in = null; try { // Connect to port 79 (the standard finger port) on the host. String hostname = "www.java2s.com"; s = new Socket(hostname, 79); // Set up the streams out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); in = new BufferedReader(new InputStreamReader(s.getInputStream())); // Send a blank line to the finger server, telling it that we want // a listing of everyone logged on instead of information about an // individual user. out.print("\n"); out.flush(); // Send it out // Now read the server's response // The server should send lines terminated with \n or \r. String line; while ((line = in.readLine()) != null) { System.out.println(line); } System.out.println("Who's Logged On: " + hostname); } catch (IOException e) { System.out.println("Who's Logged On: Error"); } // Close the streams! finally { try { in.close(); out.close(); s.close(); } catch (Exception e) { } } }
From source file:GetMessageExample.java
public static void main(String args[]) throws Exception { if (args.length != 3) { System.err.println("Usage: java MailExample host username password"); System.exit(-1);/*from w w w. j a va 2 s . c o m*/ } String host = args[0]; String username = args[1]; String password = args[2]; // Create empty properties Properties props = new Properties(); // Get session Session session = Session.getDefaultInstance(props, null); // Get the store Store store = session.getStore("pop3"); store.connect(host, username, password); // Get folder Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Get directory Message message[] = folder.getMessages(); for (int i = 0, n = message.length; i < n; i++) { System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject()); System.out.println("Read message? [YES to read/QUIT to end]"); String line = reader.readLine(); if ("YES".equalsIgnoreCase(line)) { System.out.println(message[i].getContent()); } else if ("QUIT".equalsIgnoreCase(line)) { break; } } // Close connection folder.close(false); store.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile")); DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey key = skf.generateSecret(ks); Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject())); CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c); BufferedReader br = new BufferedReader(new InputStreamReader(cis)); System.out.println(br.readLine()); }
From source file:FactQuoter.java
public static void main(String[] args) throws IOException { // This is how we set things up to read lines of text from the user. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Loop forever for (;;) {/* w ww .j a v a 2 s . com*/ // Display a prompt to the user System.out.print("FactQuoter> "); // Read a line from the user String line = in.readLine(); // If we reach the end-of-file, // or if the user types "quit", then quit if ((line == null) || line.equals("quit")) break; // Try to parse the line, and compute and print the factorial try { int x = Integer.parseInt(line); System.out.println(x + "! = " + Factorial4.factorial(x)); } // If anything goes wrong, display a generic error message catch (Exception e) { System.out.println("Invalid Input"); } } }
From source file:ExecDemoWait.java
public static void main(String argv[]) throws IOException { // A Runtime object has methods for dealing with the OS Runtime r = Runtime.getRuntime(); Process p; // Process tracks one external native process BufferedReader is; // reader for output of process String line;//from ww w.ja v a 2 s . c o m // Our argv[0] contains the program to run; remaining elements // of argv contain args for the target program. This is just // what is needed for the String[] form of exec. p = r.exec(argv); System.out.println("In Main after exec"); // getInputStream gives an Input stream connected to // the process p's standard output. Just use it to make // a BufferedReader to readLine() what the program writes out. is = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = is.readLine()) != null) System.out.println(line); System.out.println("In Main after EOF"); System.out.flush(); try { p.waitFor(); // wait for process to complete } catch (InterruptedException e) { System.err.println(e); // "Can'tHappen" return; } System.err.println("Process done, exit status was " + p.exitValue()); return; }