Example usage for java.io BufferedReader close

List of usage examples for java.io BufferedReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:URLReader.java

public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));

    String inputLine;//from   ww  w.  j ava2s  . com

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    URL url = new URL("http://server.com:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);/* w w w . j a v  a  2s  . co m*/
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    char[] passphrase = "password".toCharArray();
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(".keystore"), passphrase);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(keystore, passphrase);/*from  w ww  .j  ava2  s. c o m*/
    SSLContext context = SSLContext.getInstance("TLS");
    KeyManager[] keyManagers = kmf.getKeyManagers();

    context.init(keyManagers, null, null);

    SSLServerSocketFactory ssf = context.getServerSocketFactory();
    ServerSocket ss = ssf.createServerSocket(PORT);

    Socket s = ss.accept();

    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String line = null;
    while (((line = in.readLine()) != null)) {
        System.out.println(line);
    }
    in.close();
    s.close();
}

From source file:Main.java

public static void main(String... args) throws IOException {
    URL url = new URL("http://java2s.com");
    InputStream is = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer sb = new StringBuffer();

    String line;/*  w ww  .j a va 2  s.  c  o m*/

    while ((line = br.readLine()) != null)
        sb.append(line + System.lineSeparator());

    br.close();

    System.out.print(sb.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    Socket socket = new Socket("127.0.0.1", 8080);

    String path = "/servlet";
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);/*from  w w  w . j a va2 s  .  c  o  m*/
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String strLine = in.readLine();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write(strLine, 0, strLine.length());
    out.flush();//from w w  w  .  ja  v a2  s  . c  o m
    in.close();
    out.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from w  w w  .  j  av a  2  s.  c o m*/
        BufferedReader input = new BufferedReader(new FileReader(args[0]));
        ArrayList list = new ArrayList();
        String line;
        while ((line = input.readLine()) != null) {
            list.add(line);
        }
        input.close();

        Collections.reverse(list);

        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
        for (Iterator i = list.iterator(); i.hasNext();) {
            output.println((String) i.next());
        }
        output.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");

    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;//from   ww  w.  j ava2 s.com
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();

    System.out.println("Checksum: " + csum.getChecksum().getValue());

    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String first, last = null, line;
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    first = in.readLine();//  ww  w .j a  v  a2 s.c  o  m
    while ((line = in.readLine()) != null) {
        if (line != null) {
            last = line;
        }
    }
    System.out.println(first + "\n" + last);
    in.close();
}

From source file:Reverse.java

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

    if (args.length != 1) {
        System.err.println("Usage:  java Reverse " + "string_to_reverse");
        System.exit(1);/*from w w  w .  j  a  v  a  2s .c  o m*/
    }

    String stringToReverse = URLEncoder.encode(args[0]);

    URL url = new URL("http://java.sun.com/cgi-bin/backwards");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.println("string=" + stringToReverse);
    out.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();
}