Example usage for java.io BufferedReader readLine

List of usage examples for java.io BufferedReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Reads a line of text.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File archivo = new File("c:/Java_Dev/run.bat");
    FileReader fr = new FileReader(archivo);
    BufferedReader br = new BufferedReader(fr);

    Vector<String> lines = new Vector<String>();

    String line;/*  w  w w. ja va 2 s.  c  o m*/
    while ((line = br.readLine()) != null) {
        lines.add(line);
    }
    JOptionPane.showMessageDialog(null, new JScrollPane(new JList(lines)));
    fr.close();
}

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 a  2  s . c o m
            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 {
    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 av  a  2  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:MainClass.java

public static void main(String[] args) {
    try {/*from w  ww. java  2s . co  m*/

        URL url = new URL("http://www.java2s.com/");
        URLConnection urlConnection = url.openConnection();
        Map<String, List<String>> headers = urlConnection.getHeaderFields();
        Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
        for (Map.Entry<String, List<String>> entry : entrySet) {
            String headerName = entry.getKey();
            System.out.println("Header Name:" + headerName);
            List<String> headerValues = entry.getValue();
            for (String value : headerValues) {
                System.out.print("Header value:" + value);
            }
            System.out.println();
            System.out.println();
        }
        InputStream inputStream = urlConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = bufferedReader.readLine();
        while (line != null) {
            System.out.println(line);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:ChatClient.java

  public static void main(String[] args) throws Exception {
  DatagramSocket s = new DatagramSocket();
  byte[] buf = new byte[1000];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);

  InetAddress hostAddress = InetAddress.getByName("localhost");
  while (true) {/*  ww  w . j a v  a  2 s. com*/
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String outMessage = stdin.readLine();

    if (outMessage.equals("bye"))
      break;
    String outString = "Client say: " + outMessage;
    buf = outString.getBytes();

    DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999);
    s.send(out);

    s.receive(dp);
    String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": "
        + new String(dp.getData(), 0, dp.getLength());
    System.out.println(rcvd);
  }
}

From source file:AnotherBeerServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    System.out.println("Listening");
    Socket sock = ssock.accept();
    ssock.close(); // no more connects

    PrintStream ps = new PrintStream(sock.getOutputStream());

    // ask for count
    ps.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));

    // read and parse it
    String line = input.readLine();
    ps.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        ps.println(i + " Java Source and Support.");
    }/* w w w .  j a va 2  s.c o  m*/
    ps.close();
    sock.close();
}

From source file:LowerCaseReader.java

public static void main(String[] args) throws Exception {
    String fileName = "test.txt";
    LowerCaseReader lcr = new LowerCaseReader(new FileReader(fileName));
    int c = -1;//from   w  w  w  .  j  a v a2s.  co  m
    while ((c = lcr.read()) != -1) {
        System.out.print((char) c);
    }
    lcr.close();
    BufferedReader br = new BufferedReader(new LowerCaseReader(new FileReader(fileName)));
    String str = null;
    while ((str = br.readLine()) != null) {
        System.out.println(str);
    }
    br.close();
}

From source file:edu.usc.ee599.CommunityStats.java

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

    File dir = new File("results5");
    PrintWriter writer = new PrintWriter(new FileWriter("results5_stats.txt"));

    File[] files = dir.listFiles();

    DescriptiveStatistics statistics1 = new DescriptiveStatistics();
    DescriptiveStatistics statistics2 = new DescriptiveStatistics();
    for (File file : files) {

        BufferedReader reader = new BufferedReader(new FileReader(file));

        String line1 = reader.readLine();
        String line2 = reader.readLine();

        int balanced = Integer.parseInt(line1.split(",")[1]);
        int unbalanced = Integer.parseInt(line2.split(",")[1]);

        double bp = (double) balanced / (double) (balanced + unbalanced);
        double up = (double) unbalanced / (double) (balanced + unbalanced);

        statistics1.addValue(bp);//w ww.jav a2 s  .  c  o m
        statistics2.addValue(up);

    }

    writer.println("AVG Balanced %: " + statistics1.getMean());
    writer.println("AVG Unbalanced %: " + statistics2.getMean());

    writer.println("STD Balanced %: " + statistics1.getStandardDeviation());
    writer.println("STD Unbalanced %: " + statistics2.getStandardDeviation());

    writer.flush();
    writer.close();

}

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  ww.j  ava2s  . c om
    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:com.rdio.simple.examples.CommandLine.java

public static void main(String[] args) throws IOException, JSONException {
    ConsumerCredentials consumerCredentials = new ConsumerCredentials();
    RdioClient rdio = new RdioCoreClient(consumerCredentials);

    try {//from  w  ww  .j  a  va 2  s. c om
        RdioClient.AuthState authState = rdio.beginAuthentication("oob");
        System.out.println("Go to: " + authState.url);
        System.out.print("Then enter the code: ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String verifier = reader.readLine().trim();
        RdioClient.Token accessToken = rdio.completeAuthentication(verifier, authState.requestToken);
        rdio = new RdioCoreClient(consumerCredentials, accessToken);

        try {
            JSONObject response = new JSONObject(rdio.call("getPlaylists"));
            JSONArray playlists = (JSONArray) ((JSONObject) response.get("result")).get("owned");
            for (int i = 0; i < playlists.length(); i++) {
                JSONObject playlist = playlists.getJSONObject(i);
                System.out.println(playlist.getString("shortUrl") + "\t" + playlist.getString("name"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (RdioClient.RdioException e) {
        System.err.println("Rdio Error: " + e.toString());
    }
}