List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:Main.java
public static String ReadAllText(String path) throws IOException { String returnValue = ""; BufferedReader reader = null; try {/*from w w w .jav a2 s . c o m*/ FileReader file = new FileReader(path); reader = new BufferedReader(file); String line = ""; while ((line = reader.readLine()) != null) { returnValue += line + "\n"; } } finally { if (reader != null) reader.close(); } return returnValue; }
From source file:Main.java
public static Map<String, String> getCPUInfo() { Map<String, String> cpuInfo = new HashMap<String, String>(); Runtime runtime = Runtime.getRuntime(); try {/*from w w w. j ava2 s . co m*/ Process process = runtime.exec("cat /proc/cpuinfo"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { String[] strs = line.split(":"); if (strs.length == 2) { cpuInfo.put(strs[0].trim(), strs[1].trim()); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } return cpuInfo; }
From source file:com.mycomm.dao.mydao.UserDaoTest.java
public static String txt2String(File file) { String result = ""; try {/*from www. ja va2 s.co m*/ BufferedReader br = new BufferedReader(new FileReader(file));//BufferedReader?? String s = null; while ((s = br.readLine()) != null) {//readLine result = result + "\n" + s; } br.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
/** * Reads the first line of text from the given file *///w w w . j a v a 2s .c o m public static String readOneLine(String fileName) { String line = null; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(fileName), 512); line = reader.readLine(); } catch (IOException e) { Log.e(TAG, "Could not read from file " + fileName, e); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // ignored, not much we can do anyway } } return line; }
From source file:edu.stanford.base.batch.RawCollectionsExample.java
/** * @param subcat//from w ww.jav a2 s . co m * @throws IOException * @throws HttpException * @throws JsonParseException * @throws URISyntaxException */ public static void pullSearsProducts(String subcat) throws IOException, HttpException, JsonParseException, URISyntaxException { StringBuffer reponse = new StringBuffer(); //http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&searchType=category&productsOnly=1&contentType=json //String request = "http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&subCategoryName=Side-by-Side+Refrigerators&searchType=subcategory&productsOnly=1&endIndex=1000&startIndex=1&contentType=json"; String request = "http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&subCategoryName=" + subcat + "&searchType=subcategory&productsOnly=1&contentType=json&endIndex=1000&startIndex=1"; URI uri = new URI(request); URL url = uri.toURL(); //Compact+Refrigerators System.out.println(url); HttpClient client = new HttpClient(); GetMethod method = new GetMethod(request); // Send GET request int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } InputStream rstream = null; // Get the response body rstream = method.getResponseBodyAsStream(); // Process the response from Yahoo! Web Services BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); String line; while ((line = br.readLine()) != null) { reponse.append(line); System.out.println(line); } br.close(); Gson gson = new Gson(); /* // gson.registerTypeAdapter(Event.class, new MyInstanceCreator()); Collection collection = new ArrayList(); collection.add("hello"); collection.add(5); collection.add(new Event("GREETINGS", "guest")); String json2 = gson.toJson(collection); System.out.println("Using Gson.toJson() on a raw collection: " + json2);*/ JsonParser parser = new JsonParser(); //JsonArray array = parser.parse(json1).getAsJsonArray(); String products = StringUtils.remove(reponse.toString(), "{\"mercadoresult\":{\"products\":{\"product\":[true,"); //System.out.println(products); String productsList = StringUtils.substring(products, 0, StringUtils.indexOf(products, "productcount") - 3); // System.out.println(productsList); productsList = "[" + StringUtils.replaceOnce(productsList, "}}]]", "}]]"); //System.out.println(productsList); List<SearsProduct> prodList = new ArrayList<SearsProduct>(); // Reader reader = new InputStreamReader(productsList); Gson gson1 = new GsonBuilder().create(); JsonArray array1 = parser.parse(productsList).getAsJsonArray(); JsonArray prodArray = (JsonArray) array1.get(0); // prodList= gson1.fromJson(array1.get(2), ArrayList.class); for (JsonElement jsonElement : prodArray) { prodList.add(gson.fromJson(jsonElement, SearsProduct.class)); //System.out.println(gson.fromJson(jsonElement, SearsProduct.class)); } PullSearsProductsDAO pullSearsProductsDAO = new PullSearsProductsDAO(); try { pullSearsProductsDAO.pullAllProducts(prodList); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static String fileToString(String filePath) throws Exception { File f = new File(filePath); BufferedReader br = new BufferedReader(new FileReader(f)); try {//from ww w . j a va 2 s . com StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append("\n"); line = br.readLine(); } return sb.toString(); } finally { br.close(); } }
From source file:com.francelabs.datafari.utils.SendHttpRequest.java
public static void sendGET(String url, String userAgent) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", userAgent); int responseCode = con.getResponseCode(); logger.debug("GET Response Code :: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { // success BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine;//from ww w . ja v a2 s . com StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print result logger.debug(response.toString()); } else { logger.debug("GET request not worked"); } }
From source file:localSPs.SpiderOakAPI.java
public static String connectWithREST(String url, String method) throws IOException, ProtocolException, MalformedURLException { String newURL = ""; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // Connect with a REST Method: GET, DELETE, PUT con.setRequestMethod(method);//from w ww. ja va 2 s .com //add request header con.setReadTimeout(20000); con.setConnectTimeout(20000); con.setRequestProperty("User-Agent", "Mozilla/5.0"); if (method.equals(DELETE) || method.equals(PUT)) con.addRequestProperty("Authorization", "Base M5XWW5JNONZWUMSANBXXI3LBNFWC4Y3PNU5E2NDSNFXTEMZVJ5QWW"); int responseCode = con.getResponseCode(); // Read response BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); newURL = response.toString(); return newURL; }
From source file:models.Calais.java
public static void test() throws IOException, ParseException { // Simpler test with two people and two companies // String html = "<html> <body> Mark Zuckerberg, Steve Jobs, Twitter and Apple Inc </body> </html>"; // Full test with real html String html = ""; BufferedReader reader = new BufferedReader(new FileReader("input/techcrunch.html")); while (reader.ready()) { html += reader.readLine();//from w ww.ja v a2 s. co m } reader.close(); System.out.println("html = " + html); JSONObject result = run(html); System.out.println("result = " + result); //httpClientPost.parseJSON("output/techcrunch.html.xml"); }
From source file:com.infinira.aerospike.dataaccess.util.Utils.java
public static String readTextFile(String fileName) throws IOException { StringBuffer output = new StringBuffer(); // This will reference one line at a time String line = null;//from w ww. ja v a2 s .co m try { // FileReader reads text files in the default encoding. FileReader fileReader = new FileReader(fileName); // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader = new BufferedReader(fileReader); while ((line = bufferedReader.readLine()) != null) { output.append(line); System.out.println(line); } bufferedReader.close(); } catch (FileNotFoundException ex) { logger.error("Unable to open file '" + fileName + "'"); throw ex; } catch (IOException ex) { logger.error("Error reading file '" + fileName + "'"); throw ex; } return output.toString(); }