List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:magixcel.FileMan.java
public static void printInputFile() { try {//from w w w. j a va 2 s .c o m InputStream is = new FileInputStream(INPUT_FILE_PATH); Reader isr = new InputStreamReader(is, Charsets.ISO_8859_1); BufferedReader buffReader = new BufferedReader(isr); // read lines from input file String line = buffReader.readLine(); while (line != null && line.length() > 0) { System.out.println(line); LINES_FROM_INPUT_FILE.add(line); line = buffReader.readLine(); } is.close(); isr.close(); buffReader.close(); // write lines to new file // FileOutputStream fos = new FileOutputStream(OUTPUT_FILE_DIRECTORY + "testOutput.txt"); // OutputStreamWriter osw = new OutputStreamWriter(fos, Charsets.ISO_8859_1); // BufferedWriter bw = new BufferedWriter(osw); // for (String lineOut : LINES_FROM_FILE) { // System.out.println("Writing line: " + lineOut); // bw.write(lineOut); // // } // bw.close(); // fos.close(); // osw.close(); } catch (FileNotFoundException ex) { // todo bedre hndtering System.out.println("File not found... restart the application and supply valid file"); } catch (IOException ex) { System.out.println( "Something went wrong parsing the file... restart the application and supply valid file"); } }
From source file:com.all.client.util.FileUtil.java
private static void closeReader(Reader reader) { if (reader != null) { try {// w ww . ja va 2s . c o m reader.close(); } catch (IOException e) { LOG.info(e, e); } } }
From source file:com.predic8.membrane.core.util.TextUtil.java
public static String formatXML(Reader reader, boolean asHTML) { StringWriter out = new StringWriter(); try {//from w ww . j a v a 2 s .com XMLBeautifierFormatter formatter = asHTML ? new HtmlBeautifierFormatter(out, 0) : new PlainBeautifierFormatter(out, 0); XMLBeautifier beautifier = new XMLBeautifier(formatter); beautifier.parse(reader); } catch (Exception e) { log.error("", e); } finally { try { out.close(); reader.close(); } catch (IOException e) { log.error("", e); } } return out.toString(); }
From source file:Main.java
/** * Read and return the entire contents of the supplied {@link File}. * /* w ww. j a v a 2 s . c om*/ * @param file the file containing the information to be read; may be null * @return the contents, or an empty string if the supplied reader is null * @throws IOException if there is an error reading the content */ public static String read(File file) throws IOException { if (file == null) return ""; StringBuilder sb = new StringBuilder(); boolean error = false; Reader reader = new FileReader(file); try { int numRead = 0; char[] buffer = new char[1024]; while ((numRead = reader.read(buffer)) > -1) { sb.append(buffer, 0, numRead); } } catch (IOException e) { error = true; // this error should be thrown, even if there is an error closing reader throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an error closing reader throw e; } finally { try { reader.close(); } catch (IOException e) { if (!error) throw e; } } return sb.toString(); }
From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java
public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }/* www . j av a 2 s .c om*/ InputStream instream = entity.getContent(); if (instream == null) { return ""; } if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException( "HTTP entity too large to be buffered in memory"); } String charset = getContentCharSet(entity); if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(instream, charset); StringBuilder buffer = new StringBuilder(); try { char[] tmp = new char[1024]; int l; while ((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } } finally { reader.close(); } return buffer.toString(); }
From source file:Main.java
public static String highlight(final List<String> lines, final String meta, final String prog, final String encoding) throws IOException { final File tmpIn = new File(System.getProperty("java.io.tmpdir"), String.format("txtmark_code_%d_%d.in", ID, IN_COUNT.incrementAndGet())); final File tmpOut = new File(System.getProperty("java.io.tmpdir"), String.format("txtmark_code_%d_%d.out", ID, OUT_COUNT.incrementAndGet())); try {/*from w ww.ja v a2 s . co m*/ final Writer w = new OutputStreamWriter(new FileOutputStream(tmpIn), encoding); try { for (final String s : lines) { w.write(s); w.write('\n'); } } finally { w.close(); } final List<String> command = new ArrayList<String>(); command.add(prog); command.add(meta); command.add(tmpIn.getAbsolutePath()); command.add(tmpOut.getAbsolutePath()); final ProcessBuilder pb = new ProcessBuilder(command); final Process p = pb.start(); final InputStream pIn = p.getInputStream(); final byte[] buffer = new byte[2048]; int exitCode = 0; for (;;) { if (pIn.available() > 0) { pIn.read(buffer); } try { exitCode = p.exitValue(); } catch (final IllegalThreadStateException itse) { continue; } break; } if (exitCode == 0) { final Reader r = new InputStreamReader(new FileInputStream(tmpOut), encoding); try { final StringBuilder sb = new StringBuilder(); for (;;) { final int c = r.read(); if (c >= 0) { sb.append((char) c); } else { break; } } return sb.toString(); } finally { r.close(); } } throw new IOException("Exited with exit code: " + exitCode); } finally { tmpIn.delete(); tmpOut.delete(); } }
From source file:com.vmware.bdd.specpolicy.ClusterSpecFactory.java
/** * Load and create cluster from spec file * //from w ww. java 2 s . c o m * @return cluster spec */ public static ClusterCreate loadFromFile(File file) throws FileNotFoundException { Reader fileReader = null; try { fileReader = new FileReader(file); Gson gson = new Gson(); return gson.fromJson(fileReader, ClusterCreate.class); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { logger.error("Failed to release buffer: " + e.getMessage()); } } } }
From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static String post(URL url, String data, String contentType, String user, String passwd) throws Exception { //System.out.println("POST "+url); HttpClient client = HttpClients.createDefault(); HttpPost request = new HttpPost(url.toURI()); request.setEntity(new StringEntity(data, ContentType.create(contentType, "UTF-8"))); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); context.setCredentialsProvider(credsProvider); }/*from w ww . ja v a 2s . c o m*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code == 204) return ""; if (code != 200) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); Reader reader = null; try { reader = new InputStreamReader(response.getEntity().getContent()); StringBuilder sb = new StringBuilder(); { int read; char[] cbuf = new char[1024]; while ((read = reader.read(cbuf)) != -1) { sb.append(cbuf, 0, read); } } return sb.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:IOUtil.java
/** * Unconditionally close an <code>Reader</code>. * Equivalent to {@link Reader#close()}, except any exceptions will be ignored. * * @param input A (possibly null) Reader *///from www . ja va 2 s . c o m public static void shutdownReader(final Reader input) { if (null == input) { return; } try { input.close(); } catch (final IOException ioe) { } }
From source file:org.wso2.mdm.agent.proxy.utils.ServerUtilities.java
public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException { InputStream instream = entity.getContent(); if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory."); }/*from w w w. jav a2 s. c om*/ String charset = getContentCharSet(entity); if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(instream, charset); StringBuilder buffer = new StringBuilder(); try { char[] bufferSize = new char[1024]; int length; while ((length = reader.read(bufferSize)) != -1) { buffer.append(bufferSize, 0, length); } } finally { reader.close(); } return buffer.toString(); }