Example usage for java.io InputStreamReader InputStreamReader

List of usage examples for java.io InputStreamReader InputStreamReader

Introduction

In this page you can find the example usage for java.io InputStreamReader InputStreamReader.

Prototype

public InputStreamReader(InputStream in) 

Source Link

Document

Creates an InputStreamReader that uses the default charset.

Usage

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   ww w . j  a  va 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 w w .  ja v a 2s  .  com
    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) {/*from w ww.j  a v a 2 s .c  o  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:Sequence.java

public static void main(String args[]) throws IOException {
    Vector v = new Vector(3);
    v.add(new FileInputStream("/etc/motd"));
    v.add(new FileInputStream("foo.bar"));
    v.add(new FileInputStream("/temp/john.txt"));
    Enumeration e = v.elements();
    SequenceInputStream sis = new SequenceInputStream(e);
    InputStreamReader isr = new InputStreamReader(sis);
    BufferedReader br = new BufferedReader(isr);
    String line;/*w  w  w  . j a v  a  2 s .  co m*/
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
}

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  w w w.j a v a2 s. 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);/* ww w  .j av a  2 s . c  o m*/
    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();
}

From source file:ResultSetMetaDataExample.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");
    Statement stmt = con.createStatement();

    boolean notDone = true;
    String sqlStr = null;/*from w w  w . j  a  va 2  s .co  m*/
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (notDone) {
        sqlStr = br.readLine();
        if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) {
            ResultSet rs = stmt.executeQuery(sqlStr);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int x = 1; x <= columnCount; x++) {
                String columnName = rsmd.getColumnName(x);
                System.out.print(columnName);
            }
            while (rs.next()) {
                for (int x = 1; x <= columnCount; x++) {
                    if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0)
                        System.out.print("$");
                    String resultStr = rs.getString(x);
                    System.out.print(resultStr + "\t");
                }
            }
        } else if (sqlStr.startsWith("exit"))
            notDone = false;
    }
    stmt.close();
    con.close();
}

From source file:HTTPServer.java

public static void main(String[] args) throws Exception {

    ServerSocket sSocket = new ServerSocket(1777);
    while (true) {
        System.out.println("Waiting for a client...");
        Socket newSocket = sSocket.accept();
        System.out.println("accepted the socket");

        OutputStream os = newSocket.getOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));

        String inLine = null;/*from  w w  w .  j av a  2 s  .co m*/
        while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) {
            System.out.println(inLine);
        }
        System.out.println("");

        StringBuffer sb = new StringBuffer();
        sb.append("<html>\n");
        sb.append("<head>\n");
        sb.append("<title>Java \n");
        sb.append("</title>\n");
        sb.append("</head>\n");
        sb.append("<body>\n");
        sb.append("<H1>HTTPServer Works!</H1>\n");
        sb.append("</body>\n");
        sb.append("</html>\n");

        String string = sb.toString();

        byte[] byteArray = string.getBytes();

        os.write("HTTP/1.0 200 OK\n".getBytes());
        os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes());
        os.write("Content-Type: text/html\n\n".getBytes());

        os.write(byteArray);
        os.flush();

        os.close();
        br.close();
        newSocket.close();
    }

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Date today = new Date();
    long millisecondsPerDay = 24 * 60 * 60 * 1000;

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.setIfModifiedSince((new Date(today.getTime() - millisecondsPerDay)).getTime());
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in);
    int c;//from  w w w.ja v a  2  s.  c om
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}