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 {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    while (true) {
        System.out.print("Radius? ");
        String str = br.readLine();
        double radius;
        try {//from   w  w w . j  a v a2  s . c om
            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;
        }
        System.out.println("radius " + radius);
        return;
    }
}

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;//www .ja v a2 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;//  www  . j  a  v  a 2s . 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:MainClass.java

public static void main(String args[]) {
    try {/*from   w  w w.j  a  v a  2 s  .  c  om*/
        FileReader fr = new FileReader(args[0]);
        BufferedReader br = new BufferedReader(fr);

        String s;
        while ((s = br.readLine()) != null)
            System.out.println(s);

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    st.whitespaceChars(',', ',');
    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:// ww w.j a  v  a2  s .c  om
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    fr.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    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://from  w  w  w  .ja va2  s  .c o m
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    fr.close();
}

From source file:FileReaderDemo.java

public static void main(String args[]) throws IOException {
    FileReader fr = new FileReader("FileReaderDemo.java");
    BufferedReader br = new BufferedReader(fr);
    String s;//  ww  w.  j  a v a  2s .c om

    while ((s = br.readLine()) != null) {
        System.out.println(s);
    }

    fr.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 w w  w .j a  va 2s  . 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:StreamTokenizerDemo.java

public static void main(String args[]) throws Exception {
    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://  www  . ja  va2s  . c  o  m
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }

    fr.close();
}

From source file:URLGet.java

public static void main(String[] args) {
    BufferedReader in = null;//w  w w . ja v  a 2s  . co 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");
}