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 {
    String fullURL = args[0];//  www  .ja  v a  2  s . c  o  m
    URL u = new URL(fullURL);
    URLConnection conn = u.openConnection();
    conn.setDoOutput(true);
    OutputStream theControl = conn.getOutputStream();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(theControl));
    for (int i = 1; i < args.length; i++) {
        out.write(args[i] + "\n");
    }
    out.close();
    InputStream theData = conn.getInputStream();

    String contentType = conn.getContentType();
    if (contentType.toLowerCase().startsWith("text")) {
        BufferedReader in = new BufferedReader(new InputStreamReader(theData));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }
}

From source file:Redirecting.java

public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);//w  w w  .j  a v  a  2s .  co m
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
        System.out.println(s);
    out.close(); // Remember this!
    System.setOut(console);
}

From source file:Who.java

public static void main(String[] v) {
    Socket s = null;// w w w .  j  av  a2 s  .  co m
    PrintWriter out = null;
    BufferedReader in = null;
    try {
        // Connect to port 79 (the standard finger port) on the host.
        String hostname = "www.java2s.com";
        s = new Socket(hostname, 79);
        // Set up the streams
        out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        // Send a blank line to the finger server, telling it that we want
        // a listing of everyone logged on instead of information about an
        // individual user.
        out.print("\n");
        out.flush(); // Send it out

        // Now read the server's response
        // The server should send lines terminated with \n or \r.
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("Who's Logged On: " + hostname);
    } catch (IOException e) {
        System.out.println("Who's Logged On: Error");
    }
    // Close the streams!
    finally {
        try {
            in.close();
            out.close();
            s.close();
        } catch (Exception e) {
        }
    }
}

From source file:GetMessageExample.java

public static void main(String args[]) throws Exception {
    if (args.length != 3) {
        System.err.println("Usage: java MailExample host username password");
        System.exit(-1);/*from   ww  w . j  a  v  a2  s.  c  o m*/
    }

    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Create empty properties
    Properties props = new Properties();

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i = 0, n = message.length; i < n; i++) {
        System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());

        System.out.println("Read message? [YES to read/QUIT to end]");
        String line = reader.readLine();
        if ("YES".equalsIgnoreCase(line)) {
            System.out.println(message[i].getContent());
        } else if ("QUIT".equalsIgnoreCase(line)) {
            break;
        }
    }

    // Close connection
    folder.close(false);
    store.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile"));
    DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey key = skf.generateSecret(ks);

    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
    CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c);
    BufferedReader br = new BufferedReader(new InputStreamReader(cis));
    System.out.println(br.readLine());
}

From source file:CompRcv.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;/*from   w w  w  .  j  a v a 2s .  com*/
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        try {
            line = bis.readLine();
            if (line == null)
                break;
            line = line + "\n";
            zip.write(line.getBytes(), 0, line.length());
        } catch (Exception e) {
            break;
        }
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:animalclient.AnimalClient.java

/**
 * @param args the command line arguments
 *///  ww  w . j ava  2 s . c om
public static void main(String[] arg) throws IOException, MalformedURLException, ParseException {
    // TODO code application logic here
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    String username;
    String password;

    System.out.println("Username:");
    username = br.readLine();
    System.out.println("Password:");
    password = br.readLine();

    AnimalJerseyClient client = new AnimalJerseyClient(username, password);

    System.out.println("");
    System.out.println(
            "Willkommen bei ihrer Tierhandlung fr exotische Tiere. Geben sie help ein fr weitere Informationen");

    String auswahl = br.readLine();

    if (auswahl.equals("help")) {
        System.out.println("Sie haben folgende Auswahlmglichkeiten:");
        System.out.println("1. Listen sie alle verfgbaren Tiere auf.");
        System.out.println("2. Kaufen sie ein Tier.");
        System.out.println("3. Suchen sie nach einem Tier.");
        System.out.println("4. Whrung ndern.");
        auswahl = br.readLine();
        makeDecision(client, auswahl);
    } else {
        makeDecision(client, auswahl);
    }

    System.out.print("End");
}

From source file:FactQuoter.java

public static void main(String[] args) throws IOException {
    // This is how we set things up to read lines of text from the user.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    // Loop forever
    for (;;) {/*from w w w .  j a va  2s  .c om*/
        // Display a prompt to the user
        System.out.print("FactQuoter> ");
        // Read a line from the user
        String line = in.readLine();
        // If we reach the end-of-file,
        // or if the user types "quit", then quit
        if ((line == null) || line.equals("quit"))
            break;
        // Try to parse the line, and compute and print the factorial
        try {
            int x = Integer.parseInt(line);
            System.out.println(x + "! = " + Factorial4.factorial(x));
        }
        // If anything goes wrong, display a generic error message
        catch (Exception e) {
            System.out.println("Invalid Input");
        }
    }
}

From source file:DataFileTest.java

public static void main(String[] args) {
    Employee staff = new Employee("Java Source", 35500);

    staff.raiseSalary(5.25);// w  ww . j  ava2 s.c o m

    try {
        PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
        writeData(staff, out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
        Employee e = readData(in);
        e.print();
        in.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}

From source file:ExecDemoWait.java

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

    // A Runtime object has methods for dealing with the OS
    Runtime r = Runtime.getRuntime();
    Process p; // Process tracks one external native process
    BufferedReader is; // reader for output of process
    String line;/*  w  w w .j ava2s . co m*/

    // Our argv[0] contains the program to run; remaining elements
    // of argv contain args for the target program. This is just
    // what is needed for the String[] form of exec.
    p = r.exec(argv);

    System.out.println("In Main after exec");

    // getInputStream gives an Input stream connected to
    // the process p's standard output. Just use it to make
    // a BufferedReader to readLine() what the program writes out.
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while ((line = is.readLine()) != null)
        System.out.println(line);

    System.out.println("In Main after EOF");
    System.out.flush();
    try {
        p.waitFor(); // wait for process to complete
    } catch (InterruptedException e) {
        System.err.println(e); // "Can'tHappen"
        return;
    }
    System.err.println("Process done, exit status was " + p.exitValue());
    return;
}