List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:Main.java
/** * Read the contents of a file into a single Java String. * /* w w w. j a va 2 s .co m*/ * @param filename * The file to be read in * @return a String containing the contents of filename * @throws FileNotFoundException * and IOException on a read error. These are propegated * rather than simply trapped here bcause if reading in files * is somewhat critical to the app and if something goes * wrong, the app needs to take remedial action, probably with * user feedback. */ public static String fileToString(String filename) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new FileReader(filename)); StringBuffer sb = new StringBuffer(); String str = null; while ((str = in.readLine()) != null) { // oops, that looses newlines, so put 'em back in sb.append(str); sb.append('\n'); } in.close(); return sb.toString(); }
From source file:Main.java
public static String readText(String fileName) { StringBuilder sb = new StringBuilder(); try {//from ww w . ja v a2 s . c o m BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName))); String mLine = null; mLine = reader.readLine(); while (mLine != null) { sb.append(mLine); sb.append("\n"); mLine = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); }
From source file:connector.ISConnector.java
public static JSONObject processRequest(String servlet, byte[] query) { JSONObject response = null;/* ww w .j a va2 s.co m*/ if (servlet != null && !servlet.startsWith("/")) servlet = "/" + servlet; try { // Establish HTTP connection with Identity Service URL url = new URL(CONTEXT_PATH + servlet); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //Create the form content try (OutputStream out = conn.getOutputStream()) { out.write(query); out.close(); } // Buffer the result into a string BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); response = (JSONObject) new JSONParser().parse(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return response; }
From source file:Main.java
public static String readString(File file) { int len = 0;/*w ww . j a v a 2 s. co m*/ StringBuffer str = new StringBuffer(""); try { FileInputStream is = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader in = new BufferedReader(isr); String line = null; while ((line = in.readLine()) != null) { if (len != 0) { str.append("\r\n" + line); } else { str.append(line); } len++; } in.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } return str.toString(); }
From source file:com.leosys.core.utils.HttpServiceImpl.java
public static String getMyitem(String letter, Integer page) { Properties p = new Properties(); String urls = getXmlPath();/*from w ww. j a v a2 s. c om*/ try { p.load(new FileInputStream(urls)); } catch (Exception e) { e.printStackTrace(); } String subUrl = p.getProperty("path"); String restUrl = subUrl + "model=myservice&action=getwebdomainsitebypage&page=" + page + "&pagesize=10"; if (StringUtils.isNotEmpty(letter)) { restUrl += "&letter=" + letter; } StringBuffer strBuf; strBuf = new StringBuffer(); try { URL url = new URL(restUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));//? String line = null; while ((line = reader.readLine()) != null) strBuf.append(line + " "); reader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(strBuf.toString()); return strBuf.toString(); }
From source file:com.surfs.storage.common.util.CmdUtils.java
public static String executeCmdForString(String cmd) { BufferedReader bufRead = null; try {/*w ww .j ava 2 s .c om*/ bufRead = executeCmdForReader(cmd); return bufRead.readLine(); } catch (IOException e) { LogFactory.error(e.getMessage()); } finally { if (bufRead != null) try { bufRead.close(); } catch (IOException e) { LogFactory.error(e.getMessage()); } } return null; }
From source file:info.usbo.skypetwitter.Run.java
private static void vk() throws ParseException { String url = "https://api.vk.com/method/wall.get?v=5.28&domain=Depersonilized&filter=owner&extended=1"; String line = ""; try {//w w w . j av a2 s . c om URL url2 = new URL(url); BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream(), "UTF-8")); line = reader.readLine(); reader.close(); } catch (MalformedURLException e) { // ... } catch (IOException e) { // ... } JSONObject json = (JSONObject) new JSONParser().parse(line); json = (JSONObject) new JSONParser().parse(json.get("response").toString()); JSONArray jsona = (JSONArray) new JSONParser().parse(json.get("items").toString()); vk.clear(); for (int i = 0; i < jsona.size(); i++) { vk.add(new VK(jsona.get(i).toString())); } }
From source file:fi.helsinki.cs.iot.kahvihub.conf.ConfigurationFileParser.java
public static HubConfig parseConfigurationFile(String filename) throws ConfigurationParsingException, IOException { FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder stringBuilder = new StringBuilder(); String line = null;// w w w . ja v a 2s. c o m while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } bufferedReader.close(); fileReader.close(); JSONObject root = null; try { root = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { throw new ConfigurationParsingException("The configuration file is not a proper JSON object"); } String name = getStringProperty(root, "name"); int port = getIntProperty(root, "port"); String host = getStringProperty(root, "host"); String libdir = getStringProperty(root, "libdir"); String logdir = getStringProperty(root, "logdir"); String dbdir = getStringProperty(root, "dbdir"); String dbname = getStringProperty(root, "dbname"); int dbversion = getIntProperty(root, "dbversion"); boolean debugMode = getBooleanProperty(root, "debug"); return new HubConfig(name, host, port, libdir, logdir, dbdir, dbname, dbversion, debugMode); }
From source file:net.hillsdon.reviki.webtests.TestAttachments.java
public static String getAttachmentAtEndOfLink(final HtmlAnchor link) throws IOException { UnexpectedPage attachment = (UnexpectedPage) link.click(); BufferedReader in = new BufferedReader(new InputStreamReader(attachment.getInputStream())); try {/* w w w . ja v a 2 s . co m*/ return IOUtils.toString(in).trim(); } finally { in.close(); } }
From source file:Main.java
/** * same to {@link ResourceUtils#getFileFromRaw(android.content.Context, int)}, but return type is List<String> * /*from w ww . j a v a 2 s . co m*/ * @param context * @param resId * @return */ public static List<String> getFileToListFromRaw(Context context, int resId) { if (context == null) { return null; } List<String> fileContent = new ArrayList<String>(); BufferedReader reader = null; try { InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId)); reader = new BufferedReader(in); String line = null; while ((line = reader.readLine()) != null) { fileContent.add(line); } reader.close(); return fileContent; } catch (IOException e) { e.printStackTrace(); return null; } }