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

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");
    Statement stmt = con.createStatement();

    boolean notDone = true;
    String sqlStr = null;//from   ww w.j a  va2  s.  co  m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (notDone) {
        sqlStr = br.readLine();
        if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) {
            ResultSet rs = stmt.executeQuery(sqlStr);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int x = 1; x <= columnCount; x++) {
                String columnName = rsmd.getColumnName(x);
                System.out.print(columnName);
            }
            while (rs.next()) {
                for (int x = 1; x <= columnCount; x++) {
                    if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0)
                        System.out.print("$");
                    String resultStr = rs.getString(x);
                    System.out.print(resultStr + "\t");
                }
            }
        } else if (sqlStr.startsWith("exit"))
            notDone = false;
    }
    stmt.close();
    con.close();
}

From source file:HTTPServer.java

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

    ServerSocket sSocket = new ServerSocket(1777);
    while (true) {
        System.out.println("Waiting for a client...");
        Socket newSocket = sSocket.accept();
        System.out.println("accepted the socket");

        OutputStream os = newSocket.getOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));

        String inLine = null;/*w  w w .  ja  v  a2s  .c om*/
        while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) {
            System.out.println(inLine);
        }
        System.out.println("");

        StringBuffer sb = new StringBuffer();
        sb.append("<html>\n");
        sb.append("<head>\n");
        sb.append("<title>Java \n");
        sb.append("</title>\n");
        sb.append("</head>\n");
        sb.append("<body>\n");
        sb.append("<H1>HTTPServer Works!</H1>\n");
        sb.append("</body>\n");
        sb.append("</html>\n");

        String string = sb.toString();

        byte[] byteArray = string.getBytes();

        os.write("HTTP/1.0 200 OK\n".getBytes());
        os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes());
        os.write("Content-Type: text/html\n\n".getBytes());

        os.write(byteArray);
        os.flush();

        os.close();
        br.close();
        newSocket.close();
    }

}

From source file:Print.java

public static void main(String[] args) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");

    env.put(Context.PROVIDER_URL, "file:/tmp/marketing");

    Context initCtx = new InitialContext(env);
    File f = (File) initCtx.lookup("reports/report1.txt");
    if (f != null) {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String l = null;//  w w  w.j  a  v a  2  s  .  co m
        while ((l = br.readLine()) != null)
            System.out.println(l);
    }

}

From source file:CollateApp.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.out.println("Usage: java CollateApp file");
        System.exit(0);//ww  w .ja v  a  2  s .c  om
    }
    Locale defaultLocale = Locale.getDefault();
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(defaultLocale);
    Vector keyVector = new Vector();
    try {
        BufferedReader in = new BufferedReader(new FileReader(args[0]));
        String line;
        while ((line = in.readLine()) != null)
            keyVector.addElement(collator.getCollationKey(line));
        in.close();
    } catch (Exception ex) {
        System.out.println(ex);
        System.exit(0);
    }
    CollationKey keys[] = new CollationKey[keyVector.size()];
    for (int i = 0; i < keys.length; ++i)
        keys[i] = (CollationKey) keyVector.elementAt(i);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://www.java.com");
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection connection = null;
    if (urlConnection instanceof HttpURLConnection) {
        connection = (HttpURLConnection) urlConnection;
    } else {/* w w  w  .j  a v  a  2 s. c o m*/
        System.out.println("Please enter an HTTP URL.");
        return;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String urlString = "";
    String current;
    while ((current = in.readLine()) != null) {
        urlString += current;
    }
    System.out.println(urlString);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Vector v = new Vector(3);
    v.add(new FileInputStream("/a/b"));
    v.add(new FileInputStream("yourfile.bar"));
    v.add(new FileInputStream("/yourfile.txt"));

    Enumeration e = v.elements();
    SequenceInputStream sis = new SequenceInputStream(e);
    InputStreamReader isr = new InputStreamReader(sis);
    BufferedReader br = new BufferedReader(isr);
    String line;//from   w ww  . j  a v  a2 s .  c  o  m
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
}

From source file:CollateApp.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.out.println("Usage: java CollateApp file");
        System.exit(0);// ww w  .j  a v a  2s  .com
    }
    Locale defaultLocale = Locale.getDefault();
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(defaultLocale);
    Vector<Object> keyVector = new Vector<Object>();
    try {
        BufferedReader in = new BufferedReader(new FileReader(args[0]));
        String line;
        while ((line = in.readLine()) != null)
            keyVector.addElement(collator.getCollationKey(line));
        in.close();
    } catch (Exception ex) {
        System.out.println(ex);
        System.exit(0);
    }
    CollationKey keys[] = new CollationKey[keyVector.size()];
    for (int i = 0; i < keys.length; ++i)
        keys[i] = (CollationKey) keyVector.elementAt(i);
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {
    String fileName = "BigFile.txt";
    FileInputStream fis = new FileInputStream(fileName);
    JLabel filenameLabel = new JLabel(fileName, JLabel.RIGHT);
    Object message[] = { "Reading:", filenameLabel };
    ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, message, fis);
    InputStreamReader isr = new InputStreamReader(pmis);
    BufferedReader br = new BufferedReader(isr);
    String line;/*from w  w w  .  j  a  v  a 2s .  c  o m*/
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();//w  w  w  .j av a 2s. c  om
    PdfContentByte cb = writer.getDirectContent();
    BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
    String line;
    Paragraph p;
    float pos;
    while ((line = reader.readLine()) != null) {
        p = new Paragraph("    " + line);
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        document.add(p);
        pos = writer.getVerticalPosition(false);
        cb.moveTo(0, pos);
        cb.lineTo(PageSize.A4.width(), pos);
        cb.stroke();
        if (pos < 90)
            document.newPage();
    }
    reader.close();
    document.close();
}

From source file:AuthDemo.java

public static void main(String args[]) throws MalformedURLException, IOException {
    String urlString = "";
    String username = "";
    String password = "";
    Authenticator.setDefault(new MyAuthenticator(username, password));
    URL url = new URL(urlString);
    InputStream content = (InputStream) url.getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(content));
    String line;/*w  w w.  ja  v a  2s  . c  om*/
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    System.out.println("Done.");
}