List of usage examples for java.io OutputStreamWriter close
public void close() throws IOException
From source file:com.krawler.common.util.BaseStringUtil.java
public static String makeExternalRequest(String urlstr, String postdata) { String result = ""; try {/*from ww w . j av a2s . co m*/ URL url = new URL(urlstr); try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); DataOutputStream d = new DataOutputStream(conn.getOutputStream()); String data = postdata; OutputStreamWriter ow = new OutputStreamWriter(d); ow.write(data); ow.close(); BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder stringbufff = new StringBuilder(); while ((inputLine = input.readLine()) != null) { stringbufff.append(inputLine); } result = stringbufff.toString(); input.close(); } catch (IOException ex) { System.out.print(ex); } } catch (MalformedURLException ex) { System.out.print(ex); } finally { return result; } }
From source file:com.tactfactory.harmony.utils.TactFileUtils.java
/** Write StringBuffer contents to the given file. * @param buff The buffer to write to the file * @param file The file in which the buffer must be copied *//* ww w. jav a 2 s . c o m*/ public static void stringBufferToFile(final StringBuffer buff, final File file) { FileOutputStream fos = null; OutputStreamWriter out = null; BufferedWriter bw = null; try { fos = new FileOutputStream(file); out = new OutputStreamWriter(fos, TactFileUtils.DEFAULT_ENCODING); bw = new BufferedWriter(out); bw.write(buff.toString()); } catch (final IOException e) { ConsoleUtils.displayError(e); } finally { try { if (bw != null) { bw.close(); } if (out != null) { out.close(); } if (fos != null) { fos.close(); } } catch (final IOException e) { ConsoleUtils.displayError(e); } } }
From source file:och.util.FileUtil.java
public static String readFile(FileInputStream fis, String charset) throws UnsupportedEncodingException, IOException { InputStreamReader r = null;// w w w. j a va2 s . c om OutputStreamWriter w = null; try { r = new InputStreamReader(fis, charset); ByteArrayOutputStream out = new ByteArrayOutputStream(); w = new OutputStreamWriter(out, charset); char[] buff = new char[1024 * 4]; int i; while ((i = r.read(buff)) > 0) { w.write(buff, 0, i); } w.flush(); return out.toString(charset); } finally { if (r != null) try { r.close(); } catch (Exception e) { e.printStackTrace(); } if (w != null) try { w.close(); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.noshufou.android.su.util.Util.java
public static boolean writeDefaultStoreFile(Context context) { File storedDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + "stored"); storedDir.mkdirs();/*from w w w. j ava 2s . c o m*/ File defFile = new File(storedDir.getAbsolutePath() + File.separator + "default"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String action = prefs.getString(Preferences.AUTOMATIC_ACTION, "prompt"); try { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(defFile.getAbsolutePath())); out.write(action); out.write("\n"); out.flush(); out.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Default file not written", e); return false; } catch (IOException e) { Log.w(TAG, "Default file not written", e); return false; } return true; }
From source file:com.noshufou.android.su.util.Util.java
public static boolean writeOptionsFile(Context context) { File storedDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + "stored"); storedDir.mkdirs();/*w w w . j a v a2 s . c o m*/ File optFile = new File(storedDir.getAbsolutePath() + File.separator + "options"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String ownerMode = prefs.getString(Preferences.USER_MODE, "owner_only"); try { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(optFile.getAbsolutePath())); out.write(ownerMode); out.write("\n"); out.flush(); out.close(); } catch (FileNotFoundException e) { Log.w(TAG, "Options file not written", e); return false; } catch (IOException e) { Log.w(TAG, "Options file not written", e); return false; } return true; }
From source file:org.csware.ee.utils.Tools.java
public static String GetDataByPost(String httpUrl, String parMap) { try {/*from w w w .ja v a 2s. com*/ URL url = new URL(httpUrl);// HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST"); // ? connection.setRequestProperty("Accept", "application/json"); // ?? connection.setRequestProperty("Content-Type", "application/json"); // ???? connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8? out.append(parMap); out.flush(); out.close(); // ?? int length = (int) connection.getContentLength();// ? InputStream is = connection.getInputStream(); if (length != -1) { byte[] data = new byte[length]; byte[] temp = new byte[1024]; int readLen = 0; int destPos = 0; while ((readLen = is.read(temp)) > 0) { System.arraycopy(temp, 0, data, destPos, readLen); destPos += readLen; } String result = new String(data, "UTF-8"); // utf-8? return result; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("aa: " + e.getMessage()); } return "error"; // ? }
From source file:org.hl7.fhir.client.ClientUtils.java
private static byte[] encodeFormSubmission(Map<String, String> parameters, String resourceName, Resource resource, String boundary) throws Exception { ByteArrayOutputStream b = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(b, "UTF-8"); for (String name : parameters.keySet()) { w.write("--"); w.write(boundary);//w w w . j a v a 2 s.co m w.write("\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n"); w.write(parameters.get(name) + "\r\n"); } w.write("--"); w.write(boundary); w.write("\r\nContent-Disposition: form-data; name=\"" + resourceName + "\"\r\n\r\n"); w.close(); new JsonComposer().compose(b, resource, false); w = new OutputStreamWriter(b, "UTF-8"); w.write("\r\n--"); w.write(boundary); w.write("--"); w.close(); return b.toByteArray(); }
From source file:adalid.commons.velocity.VelocityEngineer.java
public static void write(VelocityContext context, String tempname, String filename, String charset1, String charset2) throws Exception { charset1 = StringUtils.defaultIfBlank(charset1, getTemplateEncoding(tempname)); charset2 = StringUtils.defaultIfBlank(charset2, getDocumentEncoding(filename)); // if (!charset1.equals(VELOCITY_TEMPLATE_DEFAULT_ENCODING)) { // log(Level.INFO, "write", "template=" + tempname, "template-encoding=" + charset1, "document-encoding=" + charset2); // }//from w w w . j a v a 2 s . c o m // if (!charset1.equals(charset2)) { // log(Level.WARN, "write", "template=" + tempname, "template-encoding=" + charset1, "document-encoding=" + charset2); // } StringWriter sw = merge(context, tempname, charset1); if (sw != null) { FileOutputStream fileOutputStream; OutputStreamWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileOutputStream = new FileOutputStream(filename); fileWriter = new OutputStreamWriter(fileOutputStream, charset2); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(sw.toString()); } catch (IOException ex) { throw ex; } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException ex) { logger.fatal(ThrowableUtils.getString(ex), ex); } } if (fileWriter != null) { try { fileWriter.close(); } catch (IOException ex) { logger.fatal(ThrowableUtils.getString(ex), ex); } } } } }
From source file:com.elastica.helper.FileUtility.java
/** * Saves HTML Source.// w ww . java 2 s . c o m * * @param path * * @throws Exception */ public static void writeToFile(final String path, final String content) throws IOException { System.gc(); FileOutputStream fileOutputStream = null; OutputStreamWriter outputStreamWriter = null; BufferedWriter bw = null; try { File parentDir = new File(path).getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } fileOutputStream = new FileOutputStream(path); outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF8"); bw = new BufferedWriter(outputStreamWriter); bw.write(content); } finally { if (bw != null) { try { bw.close(); } catch (Exception e) { e.printStackTrace(); } } if (outputStreamWriter != null) { try { outputStreamWriter.close(); } catch (Exception e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }
From source file:at.tugraz.sss.serv.SSFileU.java
public static void writeFileText(final File file, final String text) throws Exception { if (SSObjU.isNull(file, text)) { throw new Exception("pars not okay"); }//w w w . java2s. c o m // final byte[] bytes = text.getBytes(); OutputStreamWriter fileOut = null; try { fileOut = new OutputStreamWriter(openOrCreateFileWithPathForWrite(file.getAbsolutePath()), Charset.forName(SSEncodingU.utf8.toString())); fileOut.write(text); // fileOut.write (bytes, 0, bytes.length); } catch (Exception error) { throw error; } finally { if (fileOut != null) { fileOut.close(); } } }