Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:Main.java

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

    int i;//  w ww.j  a  v a2 s . c  o m

    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[] args) throws IOException {
    int i;//w w w  .  java 2  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:MainClass.java

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

    String hostname = "localhost";

    Socket connection = null;/*from  w w w.j a va 2s  .c  o m*/
    connection = new Socket(hostname, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = connection.getInputStream();
    BufferedInputStream buffer = new BufferedInputStream(raw);
    InputStreamReader in = new InputStreamReader(buffer, "8859_1");
    int c;
    while ((c = in.read()) != -1) {
        if ((c >= 32 && c < 127) || c == '\t' || c == '\r' || c == '\n') {
            System.out.write(c);
        }
    }
    connection.close();

}

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();/* ww w .  ja  v 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:ObfuscatingStream.java

/**
 * Obfuscates or unobfuscates the second command-line argument, depending on
 * whether the first argument starts with "o" or "u"
 * /*from   w ww . j a  v  a  2s  .  c o m*/
 * @param args
 *            Command-line arguments
 * @throws IOException
 *             If an error occurs obfuscating or unobfuscating
 */
public static void main(String[] args) throws IOException {
    InputStream input = new ByteArrayInputStream(args[1].getBytes());
    StringBuilder toPrint = new StringBuilder();
    if (args[0].startsWith("o")) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        OutputStream out = obfuscate(bytes);
        int read = input.read();
        while (read >= 0) {
            out.write(read);
            read = input.read();
        }
        byte[] receiptBytes = bytes.toByteArray();
        for (int b = 0; b < receiptBytes.length; b++) {
            int chr = (receiptBytes[b] + 256) % 256;
            toPrint.append(HEX_CHARS[chr >>> 4]);
            toPrint.append(HEX_CHARS[chr & 0xf]);
        }
    } else if (args[0].startsWith("u")) {
        input = unobfuscate(input);
        InputStreamReader reader = new InputStreamReader(input);

        int read = reader.read();
        while (read >= 0) {
            toPrint.append((char) read);
            read = reader.read();
        }
    } else
        throw new IllegalArgumentException("First argument must start with o or u");
    System.out.println(toPrint.toString());
}

From source file:CharsetDetector.java

public static void main(String[] args) {
    File f = new File("example.txt");

    String[] charsetsToBeTested = { "UTF-8", "windows-1253", "ISO-8859-7" };

    CharsetDetector cd = new CharsetDetector();
    Charset charset = cd.detectCharset(f, charsetsToBeTested);

    if (charset != null) {
        try {/*from   w w  w. j a  v  a2 s .co  m*/
            InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
            int c = 0;
            while ((c = reader.read()) != -1) {
                System.out.print((char) c);
            }
            reader.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    } else {
        System.out.println("Unrecognized charset.");
    }
}

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, Charset.defaultCharset());

    // input stream reader is closed
    isr.close();//from   w ww . j a v  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:Main.java

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

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

    // input stream reader is closed
    isr.close();//from  w  ww  .ja  va2 s. c  om
    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:Main.java

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

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

    // input stream reader is closed
    isr.close();//from  w  w  w. j  a  v  a2  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:HrefMatch.java

public static void main(String[] args) {
    try {/*  w ww .j a va2s. c  om*/
        // get URL string from command line or use default
        String urlString;
        if (args.length > 0)
            urlString = args[0];
        else
            urlString = "http://java.sun.com";

        // open reader for URL
        InputStreamReader in = new InputStreamReader(new URL(urlString).openStream());

        // read contents into string builder
        StringBuilder input = new StringBuilder();
        int ch;
        while ((ch = in.read()) != -1)
            input.append((char) ch);

        // search for all occurrences of pattern
        String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>";
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String match = input.substring(start, end);
            System.out.println(match);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
    }
}