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:Main.java

public static void main(String[] args) throws IOException {
    char[] cbuf = new char[5];

    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // reads into the char buffer
    int i = isr.read(cbuf, 2, 3);

    // prints the number of characters
    System.out.println("Number of characters read: " + i);

    // for each character in the character buffer
    for (char c : cbuf) {
        // for empty character
        if (((int) c) == 0)
            c = '-';

        System.out.println(c);//from  w  w  w. j a  v  a2s.  c o m
    }
    isr.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.com");

    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));

    String line;//from   www.  ja v  a2s.  c  om
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }

    reader.close();
    writer.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL(args[0]);
    Reader reader = new InputStreamReader((InputStream) url.getContent());
    new ParserDelegator().parse(reader, new TextOnly(), false);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;//from ww w . j a v  a  2s  .c om

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);//  w  w w .ja  va2s .co  m
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = is.readLine()) != null)
        System.out.println(line);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
        s2 += s + "\n";
    in.close();/* w ww  . j  av  a 2 s.co  m*/
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
        System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
        out1.println(lineCount++ + ": " + s);
    out1.close();
}

From source file:Main.java

public static void main(String[] args) {
    System.out.println("Enter a number.");
    double numberFromConsole;
    try {/*from w  ww.  ja v a2  s  .  c o  m*/
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        DecimalFormat df = new DecimalFormat();
        Number n = df.parse(s);
        numberFromConsole = n.doubleValue();
    } catch (IOException e) {
        numberFromConsole = 0;
    } catch (ParseException e) {
        numberFromConsole = 0;
    }
    System.out.println(numberFromConsole);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String hostname = "localhost";

    Socket theSocket = new Socket(hostname, 7);
    BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
    BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(theSocket.getOutputStream());
    System.out.println("Connected to echo server");

    while (true) {
        String theLine = userIn.readLine();
        if (theLine.equals("."))
            break;
        out.println(theLine);//from   www .j a  v  a  2  s .co  m
        out.flush();
        System.out.println(networkIn.readLine());
    }
    networkIn.close();
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    InetAddress server = InetAddress.getByName("localhost");
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    DatagramSocket theSocket = new DatagramSocket();
    while (true) {
        String theLine = userInput.readLine();
        if (theLine.equals("."))
            break;
        byte[] data = theLine.getBytes();
        DatagramPacket theOutput = new DatagramPacket(data, data.length, server, 99999);
        theSocket.send(theOutput);//from   w w w . j a  v a2 s.c  om
    }

}

From source file:Main.java

public static void main(String... args) throws IOException {
    URL url = new URL("http://java2s.com");
    InputStream is = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer sb = new StringBuffer();

    String line;/*w  w w  .java 2s . c om*/

    while ((line = br.readLine()) != null)
        sb.append(line + System.lineSeparator());

    br.close();

    System.out.print(sb.toString());
}