List of usage examples for java.io BufferedReader BufferedReader
public BufferedReader(Reader in)
From source file:Grep.java
public static void main(String args[]) throws Exception { String regex = ""; InputStream in = System.in; regex = args[0];// w ww . jav a2 s . com in = new BufferedInputStream(new FileInputStream(args[1])); Pattern p = null; p = Pattern.compile(regex); BufferedReader buff = new BufferedReader(new InputStreamReader(in)); String a; for (a = buff.readLine(); a != null; a = buff.readLine()) { Matcher m = p.matcher(a); if (m.find()) { System.out.println(a); } } }
From source file:AnotherBeerServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); Socket sock = ssock.accept(); ssock.close(); // no more connects PrintStream ps = new PrintStream(sock.getOutputStream()); // ask for count ps.print("count? "); BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream())); // read and parse it String line = input.readLine(); ps.println(""); int count = Integer.parseInt(line); for (int i = count; i >= 0; i--) { ps.println(i + " Java Source and Support."); }// w ww .j av a2s. c o m ps.close(); sock.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com"); URLConnection conn = url.openConnection(); conn.setDoOutput(true);//from w ww.j a va 2s. c om OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write("value=1&anotherValue=1"); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { System.out.println(line); } writer.close(); reader.close(); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8080); ss.setNeedClientAuth(true);/* ww w . j a v a 2 s . com*/ while (true) { try { Socket s = ss.accept(); OutputStream out = s.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while (((line = in.readLine()) != null) && (!("".equals(line)))) { System.out.println(line); } System.out.println(""); StringBuffer buffer = new StringBuffer(); buffer.append("<HTML>\n"); buffer.append("<HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n"); buffer.append("<BODY>\n"); buffer.append("<H1>Success!</H1>\n"); buffer.append("</BODY>\n"); buffer.append("</HTML>\n"); String string = buffer.toString(); byte[] data = string.getBytes(); out.write("HTTP/1.0 200 OK\n".getBytes()); out.write(new String("Content-Length: " + data.length + "\n").getBytes()); out.write("Content-Type: text/html\n\n".getBytes()); out.write(data); out.flush(); out.close(); in.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { ProcessBuilder launcher = new ProcessBuilder(); Map<String, String> environment = launcher.environment(); launcher.redirectErrorStream(true);//from w w w .j a v a 2s . c o m launcher.directory(new File("c:\\")); environment.put("name", "var"); launcher.command("notepad.exe"); Process p = launcher.start(); // And launch a new process BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = output.readLine()) != null) System.out.println(line); // The process should be done now, but wait to be sure. p.waitFor(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String pageAddr = "http://www.google.com/index.htm"; URL url = new URL(pageAddr); String websiteAddress = url.getHost(); String file = url.getFile();/* w ww . j a va2s .c o m*/ Socket clientSocket = new Socket(websiteAddress, 80); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); OutputStreamWriter outWriter = new OutputStreamWriter(clientSocket.getOutputStream()); outWriter.write("GET " + file + " HTTP/1.0\r\n\n"); outWriter.flush(); BufferedWriter out = new BufferedWriter(new FileWriter(file)); boolean more = true; String input; while (more) { input = inFromServer.readLine(); if (input == null) more = false; else { out.write(input); } } out.close(); clientSocket.close(); }
From source file:ChatClient.java
public static void main(String[] args) throws Exception { DatagramSocket s = new DatagramSocket(); byte[] buf = new byte[1000]; DatagramPacket dp = new DatagramPacket(buf, buf.length); InetAddress hostAddress = InetAddress.getByName("localhost"); while (true) {/* w w w . j a va2s .co m*/ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String outMessage = stdin.readLine(); if (outMessage.equals("bye")) break; String outString = "Client say: " + outMessage; buf = outString.getBytes(); DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999); s.send(out); s.receive(dp); String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": " + new String(dp.getData(), 0, dp.getLength()); System.out.println(rcvd); } }
From source file:Main.java
public static void main(String[] args) throws Exception { URL webSvcGetURL = new URL("http://www.server.net/Webservices"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(webSvcGetURL.openStream())); SAXSource saxSource = new SAXSource(new InputSource(bufferedReader)); String curDir = new File(".").getCanonicalPath(); StreamSource xlstStreamSource = new StreamSource(new File(curDir + File.separator + "style.xsl")); File resultHTMLFile = new File(curDir + File.separator + "output.html"); StreamResult streamResult = new StreamResult(resultHTMLFile); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(xlstStreamSource); transformer.transform((Source) saxSource, streamResult); }
From source file:Main.java
public static void main(String[] args) throws Exception { System.out.println("Count number of rows in a specific table!"); Connection con = null;//from ww w.j av a2s . co m int count = 0; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ResultSet res = st.executeQuery("SELECT COUNT(*) FROM EMP"); while (res.next()) { count = res.getInt(1); } System.out.println("Number of row:" + count); }
From source file:Finger.java
public static void main(String[] arguments) throws Exception { StringTokenizer split = new StringTokenizer(arguments[0], "@"); String user = split.nextToken(); String host = split.nextToken(); Socket digit = new Socket(host, 79); digit.setSoTimeout(20000);// w w w. j av a 2 s . c om PrintStream out = new PrintStream(digit.getOutputStream()); out.print(user + "\015\012"); BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream())); boolean eof = false; while (!eof) { String line = in.readLine(); if (line != null) System.out.println(line); else eof = true; } digit.close(); }