Example usage for java.io BufferedReader BufferedReader

List of usage examples for java.io BufferedReader BufferedReader

Introduction

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

Prototype

public BufferedReader(Reader in) 

Source Link

Document

Creates a buffering character-input stream that uses a default-sized input buffer.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("ravi.txt"));
    while (true) {
        String line = br.readLine();
        if (line == null)
            break;
        if (!UCODE_PATTERN.matcher(line).matches()) {
            System.err.println("Bad input: " + line);
        } else {// w  w w . j  ava  2s. com
            String hex = line.substring(2, 6);
            int number = Integer.parseInt(hex, 16);
            System.out.println(hex + " -> " + ((char) number));
        }
    }
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    String line;/*from w  w w . j  a va 2s  .  c o  m*/
    Process p = Runtime.getRuntime().exec("psql -U username -d dbname -h serverhost -f scripfile.sql");
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    URL u = new URL(args[0]);
    URLConnection uc = u.openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    String s = br.readLine();//w w w. ja va  2s .  com
    while (s != null) {
        System.out.println(s);
        s = br.readLine();
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    try {// www.  jav  a2s. c  o m

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        while (true) {

            System.out.print("Radius? ");

            String str = br.readLine();
            double radius;
            try {
                radius = Double.valueOf(str).doubleValue();
            } catch (NumberFormatException nfe) {
                System.out.println("Incorrect format!");
                continue;
            }

            if (radius <= 0) {
                System.out.println("Radius must be positive!");
                continue;
            }

            double area = Math.PI * radius * radius;
            System.out.println("Area is " + area);
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher("\\D");

    String line = null;//w  ww. j  av a 2 s .  c  o m
    while ((line = rd.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            // line matches the pattern
        }
    }
}

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  w w w.  j  a va  2 s  .c o m*/
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }

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

From source file:MainClass.java

public static void main(String args[]) {

    try {/* w w w .  j  a  va2 s. c o m*/
        FileReader fr = new FileReader(args[0]);
        BufferedReader br = new BufferedReader(fr);
        StreamTokenizer st = new StreamTokenizer(br);

        st.ordinaryChar('.');

        st.wordChars('\'', '\'');

        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            switch (st.ttype) {
            case StreamTokenizer.TT_WORD:
                System.out.println(st.lineno() + ") " + st.sval);
                break;
            case StreamTokenizer.TT_NUMBER:
                System.out.println(st.lineno() + ") " + st.nval);
                break;
            default:
                System.out.println(st.lineno() + ") " + (char) st.ttype);
            }
        }

        fr.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:MainClass.java

public static void main(String args[]) {

    try {/*from w ww . j  a  va2  s  . c om*/
        FileReader fr = new FileReader(args[0]);

        BufferedReader br = new BufferedReader(fr);

        StreamTokenizer st = new StreamTokenizer(br);

        // Consider end-of-line as a token
        st.eolIsSignificant(true);

        // Declare variable to count lines
        int lines = 1;

        // Process tokens
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            switch (st.ttype) {
            case StreamTokenizer.TT_EOL:
                ++lines;
            }
        }

        System.out.println("There are " + lines + " lines");

        fr.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

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 a2  s.  c o m

    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);/*from  w w w  .ja  v a 2 s . c o m*/
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = is.readLine()) != null)
        System.out.println(line);
}