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

public static void main(String args[]) {
    Reader reader;
    int ch;//from w w  w  .ja va 2  s . com
    System.out.println("Start! " + new Date());
    try {
        reader = new FileReader(args[0]);
        while ((ch = reader.read()) != -1) {
            // read entire file
        }
    } catch (IOException io) {
        System.err.println(io);
        System.exit(-1);
    }
    System.out.println("Stop! " + new Date());
}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {//from  ww w  .  j a v a2  s.  com
        // read the first five chars
        for (int i = 0; i < 5; 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 {/*from   ww w .  j  av  a2 s  .  c o  m*/
        // read the first five chars
        for (int i = 0; i < 5; 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 {//from   w  w  w .j ava2 s.  com
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();

            // skip a char every time
            reader.skip(1);

            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";

    // create a new StringReader
    Reader reader = new StringReader(s);

    try {/*from   w ww  . j ava 2  s.c o m*/
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        System.out.println(reader.markSupported());
        reader.close();

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

From source file:com.discursive.jccook.slide.GetExample.java

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    String url = "http://www.discursive.com/jccook/dav/test.html";
    Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass");

    // List resources in top directory
    WebdavResource resource = new WebdavResource(url, credentials);

    System.out.println("The three ways to Read resources.");

    // Read to a temporary file
    File tempFile = new File(resource.getName());
    resource.getMethod(tempFile);/*from   w  ww . j  a v a2  s .  co m*/
    System.out.println("1. " + resource.getName() + " saved in file: " + tempFile.toString());

    // Read as a String
    String resourceData = resource.getMethodDataAsString();
    System.out.println("2. Contents of " + resource.getName() + " as String.");
    System.out.println(resourceData);

    // Read with a stream
    InputStream resourceStream = resource.getMethodData();
    Reader reader = new InputStreamReader(resourceStream);
    StringWriter writer = new StringWriter();
    while (reader.ready()) {
        writer.write(reader.read());
    }
    System.out.println("3. Contents of " + resource.getName() + " from InputStream.");
    System.out.println(writer.toString());

    resource.close();

}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    try {/*from   w  w w. j  a  v a 2 s. c o  m*/
        // check if reader is ready
        System.out.println(reader.ready());

        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static byte[] charArrayToByteArray(char[] buffer) {
    try {//  w  ww  .j av  a  2 s  .c  o m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(baos, ENCODING);
        Reader reader = new CharArrayReader(buffer);
        for (int ch; (ch = reader.read()) != -1;) {
            out.write(ch);
        }
        return baos.toByteArray();
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;//w ww . j  a v  a  2  s. c  om
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Slurps a Reader into a String and strips the character 0.
 * @param in//from w w w. j  ava 2s  .c  o m
 * @throws IOException
 */
public static String readerToPrintableCharString(final Reader in) throws IOException {
    StringBuffer out = new StringBuffer();
    int n;
    while ((n = in.read()) != -1) {
        if (n != 0) {
            char c = (char) n;
            out.append(c);
        }
    }
    return out.toString();
}