List of usage examples for java.io BufferedReader readLine
public String readLine() throws IOException
From source file:org.salever.rcp.remoteSystem.server.util.ClientAbortMethod.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {/*from w w w . ja va 2 s . c o m*/ String queryString = "wd=?ddd="; String decodeUrl = URLEncoder.encode(queryString, "GBK"); String string = new String(decodeUrl); System.out.println("--------------" + string + "--------------------------"); HttpGet httpget = new HttpGet("http://www.baidu.com/s?" + string); System.out.println("executing request " + httpget.getURI()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String line; while ((line = reader.readLine()) != null) { // System.out.println(line); } } System.out.println("----------------------------------------"); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.jff.searchapicluster.server.mina.Server.java
public static void main(String[] args) throws Throwable { org.apache.commons.configuration.Configuration config = new PropertiesConfiguration("settings.txt"); int serverPort = config.getInt("listenPort"); NioSocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("com/jff/searchapicluster/core/mina/codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.setHandler(new ServerSessionHandler()); acceptor.bind(new InetSocketAddress(serverPort)); System.out.println("Listening on port " + serverPort); System.out.println("Please enter filename"); // open up standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String filepath = br.readLine(); filepath = "/Users/admin/repos/study_repos/kurs_sp/search_api_cluster/SearchApiClusterServer/task.json"; File file = new File(filepath); Logger.d(LOG_TAG, file.getAbsolutePath()); if (file.exists() && file.isFile()) { Gson gson = new Gson(); SearchTask taskMessage = gson.fromJson(new FileReader(file), SearchTask.class); ServerManager serverManager = ServerManager.getInstance(); Logger.d(LOG_TAG, taskMessage.toString()); serverManager.startProcessTask(taskMessage); } else {//from w ww. ja v a 2s .com Logger.d(LOG_TAG, filepath + "not found"); } } }
From source file:WebClient.java
public static void main(String[] args) throws Exception { CookieStore store = new MyCookieStore(); CookiePolicy policy = new MyCookiePolicy(); CookieManager handler = new CookieManager(store, policy); CookieHandler.setDefault(handler); URL url = new URL("http://localhost:8080/cookieTest.jsp"); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String input;//from w ww.j a va 2 s. c o m while ((input = reader.readLine()) != null) { System.out.println(input); } reader.close(); }
From source file:cn.lynx.emi.license.GenerateLicense.java
public static void main(String[] args) throws ClassNotFoundException, ParseException { if (args == null || args.length != 4) { System.err.println("Please provide [machine code] [cpu] [memory in gb] [yyyy-MM-dd] as parameter"); return;/* w ww . j a v a2s.c o m*/ } InputStream is = GenerateLicense.class.getResourceAsStream("/privatekey"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String key = null; try { key = br.readLine(); } catch (IOException e) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); e.printStackTrace(); return; } if (key == null) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); return; } String machineCode = args[0]; int cpu = Integer.parseInt(args[1]); long mem = Long.parseLong(args[2]) * 1024 * 1024 * 1024; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); long expDate = sdf.parse(args[3]).getTime(); LicenseBean lb = new LicenseBean(); lb.setCpuCount(cpu); lb.setMemCount(mem); lb.setMachineCode(machineCode); lb.setExpireDate(expDate); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(baos); os.writeObject(lb); os.close(); String serializedLicense = Base64.encodeBase64String(baos.toByteArray()); System.out.println("License:" + encrypt(key, serializedLicense)); } catch (IOException e) { e.printStackTrace(); } }
From source file:SSLSimpleClient.java
public static void main(String[] args) throws Exception { SocketFactory sf = SSLSocketFactory.getDefault(); Socket s = sf.createSocket(args[0], Integer.parseInt(args[1])); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.println("from java2s."); pw.flush();//from ww w.ja v a 2s . c om System.out.println(br.readLine()); s.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String filename = "infile.txt"; String patternStr = "pattern"; BufferedReader rd = new BufferedReader(new FileReader(filename)); Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher("\\D"); String line = null;/*from w ww . jav a 2s . c o m*/ while ((line = rd.readLine()) != null) { matcher.reset(line); if (matcher.find()) { // line matches the pattern } } }
From source file:ParseDemo.java
public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str;/*from www.j a v a 2 s . co m*/ int i; int sum = 0; System.out.println("Enter numbers, 0 to quit."); do { str = br.readLine(); try { i = Integer.parseInt(str); } catch (NumberFormatException e) { System.out.println("Invalid format"); i = 0; } sum += i; System.out.println("Current sum is: " + sum); } while (i != 0); }
From source file:postenergy.TestHttpClient.java
public static void main(String[] args) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin"); try {// ww w. ja v a 2 s .co m List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("Email", "youremail")); nameValuePairs.add(new BasicNameValuePair("Passwd", "yourpassword")); nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE")); nameValuePairs.add(new BasicNameValuePair("source", "Google-cURL-Example")); nameValuePairs.add(new BasicNameValuePair("service", "ac2dm")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); if (line.startsWith("Auth=")) { String key = line.substring(5); // do something with the key } } } catch (IOException e) { e.printStackTrace(); } }
From source file:FileList.java
public static void main(String[] args) { final int assumedLineLength = 50; File file = new File(args[0]); List<String> fileList = new ArrayList<String>((int) (file.length() / assumedLineLength) * 2); BufferedReader reader = null; int lineCount = 0; try {//ww w .j a va 2 s. c o m reader = new BufferedReader(new FileReader(file)); for (String line = reader.readLine(); line != null; line = reader.readLine()) { fileList.add(line); lineCount++; } } catch (IOException e) { System.err.format("Could not read %s: %s%n", file, e); System.exit(1); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } int repeats = Integer.parseInt(args[1]); Random random = new Random(); for (int i = 0; i < repeats; i++) { System.out.format("%d: %s%n", i, fileList.get(random.nextInt(lineCount - 1))); } }
From source file:ReaderIter.java
public static void main(String[] args) throws IOException { // The RE pattern Pattern patt = Pattern.compile("[A-Za-z][a-z]+"); // A FileReader (see the I/O chapter) BufferedReader r = new BufferedReader(new FileReader("ReaderIter.java")); // For each line of input, try matching in it. String line;/*from w w w. j av a 2s. c om*/ while ((line = r.readLine()) != null) { // For each match in the line, extract and print it. Matcher m = patt.matcher(line); while (m.find()) { // Simplest method: // System.out.println(m.group(0)); // Get the starting position of the text int start = m.start(0); // Get ending position int end = m.end(0); // Print whatever matched. // Use CharacterIterator.substring(offset, end); System.out.println(line.substring(start, end)); } } }