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 Exception {
    URL myURL = new URL("http://www.google.com");
    BufferedReader so = new BufferedReader(new InputStreamReader(myURL.openStream()));
    while (true) {
        String output = so.readLine();
        if (output != null) {
            System.out.println(output);
        } else {//from  w  w w.ja va  2s . c o  m
            break;
        }
    }
    so.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80/index.html");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;//from   w  w w  .  ja v a 2 s  . c o  m
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
    in.close();
}

From source file:Main.java

public static void main(String[] ap) {
    String line = null;//from   ww w . ja va 2  s  .  c o m
    int val = 0;
    try {
        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
        line = is.readLine();
        val = Integer.parseInt(line);
    } catch (NumberFormatException ex) {
        System.err.println("Not a valid number: " + line);
    } catch (IOException e) {
        System.err.println("Unexpected IO ERROR: " + e);
    }
    System.out.println("I read this number: " + val);
}

From source file:Main.java

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

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

    // input stream reader is closed
    isr.close();/*from  w ww  .  j  av a 2 s.  c  o m*/
    System.out.print("close() invoked");

    // read() called after closed method
    int i = isr.read();
    char c = (char) i;
    System.out.println(c);

}

From source file:MainClass.java

public static void main(String args[]) {
    try {//from   w w  w.  j a  va  2  s . c  o  m
        System.out.print("Enter your name: ");
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);

        String name = in.readLine();
        System.out.println("Hello, " + name + ". Enter three ints...");
        int[] values = new int[3];
        double sum = 0.0;

        for (int i = 0; i < values.length; i++) {
            System.out.print("Number " + (i + 1) + ": ");
            String temp = in.readLine();
            values[i] = Integer.parseInt(temp);
            sum += values[i];
        }

        System.out.println("The average equals " + sum / values.length);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    int i;/*from  ww  w.j  a v  a2  s .c  o  m*/
    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // read till the end of the file
    while ((i = isr.read()) != -1) {
        // int to character
        char c = (char) i;
        System.out.println("Character Read: " + c);
    }
    isr.close();
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {//from  ww  w . j av  a2 s .c o m
        int token = inStream.nextToken();
        switch (token) {
        case StreamTokenizer.TT_EOF:
            System.out.println("EOF encountered.");
            eof = true;
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("EOL encountered.");
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word: " + inStream.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number: " + inStream.nval);
            break;
        default:
            System.out.println((char) token + " encountered.");
            if (token == '!')
                eof = true;
        }
    } while (!eof);
}

From source file:URLGet.java

public static void main(String[] args) {
    BufferedReader in = null;//from ww  w .jav  a2s  . c  o m
    if (args.length == 1) {
        try {
            URL url = new URL(args[0]);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = in.readLine()) != null)
                System.out.println(line);
        } catch (MalformedURLException ex) {
            System.err.println(ex);
        } catch (FileNotFoundException ex) {
            System.err.println("Failed to open stream to URL: " + ex);
        } catch (IOException ex) {
            System.err.println("Error reading URL content: " + ex);
        }
        if (in != null)
            try {
                in.close();
            } catch (IOException ex) {
            }
    } else
        System.err.println("Usage: URLGet URL");
}

From source file:Main.java

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

    int i;/*from w  w  w .  jav a  2s  .  c om*/

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

    while ((i = isr.read()) != -1) {
        char c = (char) i;
        System.out.println("Character read: " + c);
        // true if the next read is guaranteed
        boolean bool = isr.ready();

        System.out.println("Ready to read: " + bool);
    }
}

From source file:Main.java

public static void main(String[] av) throws IOException {
    StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    int i;/*w w w.jav a 2  s  . co m*/

    while ((i = tf.nextToken()) != StreamTokenizer.TT_EOF) {
        switch (i) {
        case StreamTokenizer.TT_EOF:
            System.out.println("End of file");
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("End of line");
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number " + tf.nval);
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word, length " + tf.sval.length() + "->" + tf.sval);
            break;
        default:
            System.out.println("What is it? i = " + i);
        }
    }

}