Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:MainClass.java

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

    URL u = new URL("http://www.java2s.com");
    InputStream in = u.openStream();

    in = new BufferedInputStream(in);

    Reader r = new InputStreamReader(in);
    int c;/*from www  . ja v  a  2s  . c  o m*/
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

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;//  www  .  j a v a2 s .  c o m
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:MainClass.java

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

    Authenticator.setDefault(new DialogAuthenticator());

    URL u = new URL("secure url");
    InputStream in = u.openStream();
    in = new BufferedInputStream(in);
    Reader r = new InputStreamReader(in);
    int c;/*from  ww  w  . j ava  2s . c  o  m*/
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:InputOutputDemoString.java

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

    //Read from a String s as if it were a text file:
    Reader r = new StringReader("abc");
    System.out.println("abc: " + (char) r.read() + (char) r.read() + (char) r.read());
    //Write to a StringBuffer as if it were a text file:
    Writer sw = new StringWriter();
    sw.write('d');
    sw.write('e');
    sw.write('f');
    System.out.println(sw.toString());
}

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  ww  .  j a  va 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:Main.java

public static void main(final String[] args) throws IOException {
    File infile = new File("/tmp/utf16.txt");
    FileInputStream inputStream = new FileInputStream(infile);
    Reader in = new InputStreamReader(inputStream, "UTF-16");
    int read;/*from   w  w w  .  ja v a 2s .co m*/
    while ((read = in.read()) != -1) {
        System.out.print(Character.toChars(read));
    }
    in.close();
}

From source file:MainClass.java

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

    String encoding = "ISO-8859-1";
    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int encodingStart = contentType.indexOf("charset=");
    if (encodingStart != -1) {
        encoding = contentType.substring(encodingStart + 8);
    }// w w w.  j  a va  2 s  . c o  m
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in, encoding);
    int c;
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:FileReaderWriterExample.java

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

    Reader r = new FileReader("in.txt");
    Writer w = new FileWriter("out.txt");
    int c;//from   w  w  w .  j a va2  s .  c om
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
        w.write(c);
    }
    r.close();
    w.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w  w. j  a v  a 2 s.co m
        String s = "tutorial from java2s.com";

        Reader reader = new StringReader(s);

        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.mark(10);

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.reset();

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        reader.close();

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

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {/*w  ww  . ja va  2 s .  co m*/
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print(c);
        }

        reader.mark(10);

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.reset();

        for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }

        reader.close();

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