List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:Main.java
/** * Reads content of file, and returns result as string * * @param filename path to file/*from w ww . j av a 2 s . co m*/ * @return Contents of file * @throws IOException */ public static String fileToString(String filename) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); StringBuilder builder = new StringBuilder(); String line; // For every line in the file, append it to the string builder while ((line = reader.readLine()) != null) { builder.append(line); } reader.close(); return builder.toString(); }
From source file:Main.java
/** * Read file, one line as a element of the String List * //from w w w. java 2 s. co m * @param filePath * The path of the file * @return List<String> Return file content as a String List, if the file * doesn't exist return null */ public static List<String> readFileToList(String filePath) { File file = new File(filePath); List<String> fileContent = new ArrayList<String>(); if (file != null && file.isFile()) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { fileContent.add(line); } reader.close(); return fileContent; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } return null; }
From source file:Main.java
public static String readOneLine(String sFile) { BufferedReader brBuffer; String sLine = null;/*from www. j a v a 2 s.c o m*/ try { brBuffer = new BufferedReader(new FileReader(sFile), 512); try { sLine = brBuffer.readLine(); } finally { Log.w(TAG_READ, "file " + sFile + ": " + sLine); brBuffer.close(); } } catch (Exception e) { Log.e(TAG_READ, "IO Exception when reading /sys/ file", e); } return sLine; }
From source file:Main.java
public static String readTextInputStream(InputStream is) throws IOException { StringBuffer strbuffer = new StringBuffer(); String line;//from ww w . j a v a 2s .com BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(is)); while ((line = reader.readLine()) != null) { strbuffer.append(line).append("\r\n"); } } finally { if (reader != null) { reader.close(); } } return strbuffer.toString(); }
From source file:Main.java
public static String getSDRoot() { String pre = null;/*from w w w. j a va 2s . c o m*/ String root = null; try { Runtime r = Runtime.getRuntime(); Process p = r.exec("ls mnt"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String inline; while ((inline = br.readLine()) != null) { if (inline.contains("mmcblk")) { pre = inline; break; } } br.close(); root = "/mnt/" + pre + "/" + pre + "p1"; } catch (Exception e) { } return root; }
From source file:Main.java
/** * Writes the current app logcat to a file. * * @param filename The filename to save it as * @throws IOException//from w w w. j a v a2 s . c o m */ public static void writeLogcat(String filename) throws IOException { String[] args = { "logcat", "-v", "time", "-d" }; Process process = Runtime.getRuntime().exec(args); InputStreamReader input = new InputStreamReader(process.getInputStream()); OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(filename)); BufferedReader br = new BufferedReader(input); BufferedWriter bw = new BufferedWriter(output); String line; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } bw.close(); output.close(); br.close(); input.close(); }
From source file:editor.util.URLUtil.java
public static String sendPost(String url, String data) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); // Send post request con.setDoOutput(true);//from ww w. j av a 2s .co m DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(data); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }
From source file:Main.java
public static String readFile(File file) throws IOException { StringBuffer fileData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(file)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData);/*from w w w .ja v a 2s . c om*/ buf = new char[1024]; } reader.close(); return fileData.toString(); }
From source file:com.jiubang.core.util.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * //www .jav a 2 s . c o m * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); Loger.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); Loger.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; while ((line = rd.readLine()) != null) { Loger.d(LOG_TAG, line); } rd.close(); }
From source file:fr.julienvermet.bugdroid.util.NetworkUtils.java
public static NetworkResult postJson(String url, JSONObject data) { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); int statusCode = 0; try {/*from w w w .j a va2 s. co m*/ StringEntity se = new StringEntity(data.toString()); httpPost.setEntity(se); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse response = client.execute(httpPost); StatusLine statusLine = response.getStatusLine(); statusCode = statusLine.getStatusCode(); // if (statusCode == 201) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } reader.close(); // } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } return new NetworkResult(statusCode, builder.toString()); }