List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:Main.java
private static String getData(String target, String method, String token) { try {//from w w w . ja v a 2 s . co m URL url = new URL(target); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); if (!TextUtils.isEmpty(token)) { conn.setRequestProperty("Authorization", token); } conn.setRequestMethod(method); conn.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line, data = ""; while ((line = in.readLine()) != null) { data += line; } in.close(); return data; } catch (IOException ignored) { ignored.printStackTrace(); } return null; }
From source file:com.jjtree.utilities.JServeletManager.java
public static JSONObject fetchFrom(HttpServletRequest request, String url) { JSONObject object = null;/*from w ww. ja v a2 s . com*/ try { String serverName = request.getServerName(); int portNumber = request.getServerPort(); String contextPath = request.getContextPath(); String accountUrl = "http://" + serverName + ":" + portNumber + contextPath + url; URL urldemo = new URL(accountUrl); URLConnection urlCon = urldemo.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { object = new JSONObject(inputLine); } in.close(); } catch (Exception e) { System.out.println(e); } return object; }
From source file:Main.java
public static String readFile(File f) { try {/*from w w w.jav a 2s .com*/ FileInputStream fstream = new FileInputStream(f); InputStreamReader in = new InputStreamReader(fstream); BufferedReader br = new BufferedReader(in); String strLine; StringBuilder sb = new StringBuilder(); while ((strLine = br.readLine()) != null) { sb.append(strLine); sb.append('\n'); } br.close(); in.close(); fstream.close(); return sb.toString(); } catch (Exception ex) { return ""; } }
From source file:Main.java
public static boolean addFile(InputStream src, File dest) { try {//from ww w . j av a 2 s. c o m FileOutputStream out = new FileOutputStream(dest); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(src)); String line; while ((line = bufferedReader.readLine()) != null) { out.write((line + "\n").getBytes()); } out.close(); bufferedReader.close(); return true; } catch (IOException e) { } return false; }
From source file:Main.java
private static void logErrorStream(@Nullable InputStream errorStream) throws IOException { if (errorStream == null) { return;/*w w w.j a v a 2 s. c o m*/ } StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream, "UTF-8")); try { String result; while ((result = reader.readLine()) != null) { builder.append(result); } } finally { reader.close(); } Log.w(TAG, builder.toString()); }
From source file:Main.java
public static String getPosthtml(String posturl, String postData, String encode) { String html = ""; URL url;/*from ww w .j a va2s .co m*/ try { url = new URL(posturl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("User-Agent", userAgent); String postDataStr = postData; byte[] bytes = postDataStr.getBytes("utf-8"); connection.setRequestProperty("Content-Length", "" + bytes.length); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setDoOutput(true); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); OutputStream outStrm = connection.getOutputStream(); outStrm.write(bytes); outStrm.flush(); outStrm.close(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode)); String temp = ""; while ((temp = br.readLine()) != null) { html += (temp + '\n'); } br.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:Main.java
public static long getTotalMemoryPreJB() { String str1 = "/proc/meminfo"; String str2;/*from w w w. jav a 2 s . c om*/ String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();//meminfo arrayOfString = str2.split("\\s+"); initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024; localBufferedReader.close(); return initial_memory; } catch (IOException e) { return -1; } }
From source file:Main.java
public static String readFromFile(final File file) { final StringBuilder ret = new StringBuilder(); try {//from ww w . ja v a 2 s . c o m final BufferedReader input = new BufferedReader(new FileReader(file), 4096); int len; final char buff[] = new char[4096]; while ((len = input.read(buff, 0, 4096)) != -1) { ret.append(buff, 0, len); } input.close(); } catch (final IOException exception) { exception.printStackTrace(); } return ret.toString(); }
From source file:com.itbeyond.common.EOTrackMe.java
public static int getLogFileLines() { try {//from w w w.jav a 2 s . c om BufferedReader br = new BufferedReader(new FileReader(getLogFile()), 8192); int lineCount = 0; while ((br.readLine()) != null) { lineCount++; } br.close(); return lineCount; } catch (IOException e) { } return 0; }
From source file:Main.java
public static String readFileText(Context context, String fileName, boolean deleteFile) { File root = new File(context.getExternalFilesDir(null) + File.separator); File file = new File(root, "." + fileName); StringBuilder text = new StringBuilder(); if (file.exists()) { try {/* w w w . ja v a 2 s . c o m*/ BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { text.append(line); text.append('\n'); } br.close(); if (deleteFile) file.delete(); } catch (IOException e) { } } else { text.append("File not exists"); } return text.toString(); }