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

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

    // A Runtime object has methods for dealing with the OS
    Runtime r = Runtime.getRuntime();

    // A process object tracks one external running process
    Process p;/*w  ww  .j av a2 s. co  m*/

    // file contains unsorted data
    p = r.exec("sort sortdemo.txt");

    // getInputStream gives an Input stream connected to
    // the process p's standard output (and vice versa). We use
    // that to construct a BufferedReader so we can readLine() it.
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    System.out.println("Here is your sorted data:");

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

    System.out.println("That is all, folks!");

    return;
}

From source file:com.estafeta.flujos.DemoMain.java

/**
 * Clase principal del proyecto/*  w  w  w  . j  a  v  a2  s. com*/
 * @param args argumentos de linea de comandos
 */
public static void main(String[] args) {
    // init spring context
    ApplicationContext ctx = new ClassPathXmlApplicationContext(APPLOCATION_CONTEXT_FILE);
    // get bean from context
    JmsMessageSender jmsMessageSender = (JmsMessageSender) ctx.getBean(JMS_MESSAGE_SENDER_BEAN_NAME);
    // send to a code specified destination
    Queue queue = new ActiveMQQueue(JMS_DESTINATION_NAME);
    try {
        ClassLoader classLoader = DemoMain.class.getClassLoader();
        FileInputStream fileInputStream = new FileInputStream(
                classLoader.getResource(MOCK_INPUT_FILE).getFile());
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(fileInputStream, Charset.forName(UTF_8_CHARSET)));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            jmsMessageSender.send(queue, line);
        }
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // close spring application context
    ((ClassPathXmlApplicationContext) ctx).close();
}

From source file:RestPostClient.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("http://localhost:10080/example/json/product/post");

    StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}");
    input.setContentType("application/json");
    postRequest.setEntity(input);//w ww  . j  a  va2  s.com

    HttpResponse response = httpClient.execute(postRequest);

    //if (response.getStatusLine().getStatusCode() != 201) {
    //   throw new RuntimeException("Failed : HTTP error code : "
    //      + response.getStatusLine().getStatusCode());
    //}

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();
}

From source file:com.tbodt.trp.TheRapidPermuter.java

/**
 * Main method for The Rapid Permuter./*from www  .  j  a v a2 s  .  c  o  m*/
 *
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    CommandProcessor.processCommand("\"\": remove(\"\") in([words])").ifPresent(x -> x.forEach(y -> {
    })); // to load all the classes we need

    try {
        cmd = new BasicParser().parse(options, args);
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        return;
    }

    if (Arrays.asList(cmd.getOptions()).contains(help)) { // another way commons cli is severely broken
        new HelpFormatter().printHelp("trp", options);
        return;
    }

    if (cmd.hasOption('c'))
        doCommand(cmd.getOptionValue('c'));
    else {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("trp> ");
        String input;
        while ((input = in.readLine()) != null) {
            if (input.equals("exit"))
                return; // exit
            doCommand(input);
            System.out.print("trp> ");
        }
    }
}

From source file:MainClass.java

License:asdf

public static void main(String[] args) throws IOException {
    try {//from w  w  w  .j  a va  2 s  . c om
        BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
        int lineCount = 1;
        String s = null;
        while ((s = in4.readLine()) != null)
            out1.println(lineCount++ + ": " + s);
        out1.close();
    } catch (EOFException e) {
        System.err.println("End of stream");
    }

}

From source file:com.rest.samples.getTipoCambioBanxico.java

public static void main(String[] args) {
    String url = "http://www.banxico.org.mx/tipcamb/llenarTiposCambioAction.do?idioma=sp";
    try {//from w w w.j av a  2  s  .co  m
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/5.0");
        request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

        HttpResponse res = hc.execute(request);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        Document doc = Jsoup.parse(result.toString());
        Element tipoCambioFix = doc.getElementById("FIX_DATO");
        System.out.println(tipoCambioFix.text());

    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.cncounter.test.httpclient.ProxyTunnelDemo.java

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

    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.google.com", 80);
    HttpHost proxy = new HttpHost("localhost", 1080);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {//from w  w w  .ja  v  a2  s .  c  om
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:com.dlmu.heipacker.crawler.client.ProxyTunnelDemo.java

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

    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.yahoo.com", 80);
    HttpHost proxy = new HttpHost("localhost", 8888);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {//from w  ww .j  a  v  a  2 s  .com
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:net.morphbank.webclient.ProcessFiles.java

/**
 * @param args//from ww w  .  j a va  2 s .  co  m
 *            args[0] directory including terminal '/' 
 *            args[1] prefix of request files 
 *            args[2] number of digits in file index value
 *            args[3] number of files 
 *            args[4] index of first file (default 0) 
 *            args[5] prefix of service 
 *            args[6] prefix of response files (default args[1] + "Resp")
 */
public static void main(String[] args) {
    ProcessFiles fileProcessor = new ProcessFiles();
    // restTest.processRequest(URL, UPLOAD_FILE);

    String zeros = "0000000";

    if (args.length < 4) {
        System.out.println("Too few parameters");
    } else {
        try {
            // get parameters
            String reqDir = args[0];
            String reqPrefix = args[1];
            int numDigits = Integer.valueOf(args[2]);
            if (numDigits > zeros.length())
                numDigits = zeros.length();
            NumberFormat intFormat = new DecimalFormat(zeros.substring(0, numDigits));
            int numFiles = Integer.valueOf(args[3]);
            int firstFile = 0;

            BufferedReader fileIn = new BufferedReader(new FileReader(FILE_IN_PATH));
            String line = fileIn.readLine();
            fileIn.close();
            firstFile = Integer.valueOf(line);
            // firstFile = 189;
            // numFiles = 1;
            int lastFile = firstFile + numFiles - 1;
            String url = URL;
            String respPrefix = reqPrefix + "Resp";
            if (args.length > 5)
                respPrefix = args[5];
            if (args.length > 6)
                url = args[6];

            // process files
            for (int i = firstFile; i <= lastFile; i++) {
                String xmlOutputFile = null;
                String requestFile = reqDir + reqPrefix + intFormat.format(i) + ".xml";
                System.out.println("Processing request file " + requestFile);
                String responseFile = reqDir + respPrefix + intFormat.format(i) + ".xml";
                System.out.println("Response file " + responseFile);
                // restTest.processRequest(URL, UPLOAD_FILE);
                fileProcessor.processRequest(url, requestFile, responseFile);
                Writer fileOut = new FileWriter(FILE_IN_PATH, false);
                fileOut.append(Integer.toString(i + 1));
                fileOut.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:ZipDemo.java

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; ++i) {
        String uncompressed = "";
        File f = new File(args[i]);

        if (f.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(f));

            String line = "";
            StringBuffer buffer = new StringBuffer();

            while ((line = br.readLine()) != null)
                buffer.append(line);//from   w w w.jav a 2 s  . c  om

            br.close();
            uncompressed = buffer.toString();
        } else {
            uncompressed = args[i];
        }

        byte[] compressed = ZipDemo.compress(uncompressed);

        String compressedAsString = new String(compressed);

        byte[] bytesFromCompressedAsString = compressedAsString.getBytes();

        bytesFromCompressedAsString.equals(compressed);
        System.out.println(ZipDemo.uncompress(compressed));
        System.out.println(ZipDemo.uncompress(compressedAsString));
    }
}