List of usage examples for java.io OutputStreamWriter close
public void close() throws IOException
From source file:ee.ria.xroad.common.util.PasswordStore.java
private static byte[] charToByte(char[] buffer) throws IOException { if (buffer == null) { return null; }//from w ww . ja v a 2 s. c o m ByteArrayOutputStream os = new ByteArrayOutputStream(buffer.length * 2); OutputStreamWriter writer = new OutputStreamWriter(os, UTF_8); writer.write(buffer); writer.close(); return os.toByteArray(); }
From source file:Main.java
/** * Uses http to post an XML String to a URL. * * @param url/*from w ww . j ava 2 s. c o m*/ * @param xmlString * @param return * @throws IOException * @throws UnknownHostException */ public static HttpURLConnection post(String url, String xmlString) throws IOException, UnknownHostException { // open connection URL urlObject = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\""); connection.setDoOutput(true); OutputStreamWriter outStream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); outStream.write(xmlString); outStream.close(); return connection; }
From source file:com.ms.commons.test.treedb.Objects2XmlFileUtil.java
private static void close(OutputStreamWriter writer) { if (writer != null) { try {/* w w w . j a v a 2s .c om*/ writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
/** * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8). * <p>/*from w w w .j av a 2 s. c om*/ * Contains workaround for various JVM bugs. * * @param document * The DOM * @param file * The target XML file * @throws TransformerFactoryConfigurationError * In case of an XML transformation factory configuration error * @throws TransformerException * In case of an XML transformation error * @throws IOException * In case of an I/O error */ public static void saveHumanReadable(Document document, File file) throws TransformerFactoryConfigurationError, TransformerException, IOException { // Various indentation and UTF8 encoding bugs are worked around here TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", new Integer(4)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8"); transformer.transform(new DOMSource(document), new StreamResult(writer)); writer.close(); }
From source file:Main.java
public static File writeLog(String root, String filename) { StringBuilder log = new StringBuilder(); try {/* ww w . j av a2 s . c o m*/ Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { log.append(line).append("\n"); } } catch (IOException e) { log.append(e.toString()); } File file = new File(root, filename); try { FileOutputStream fOut = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fOut); // Write the string to the file osw.write(log.toString()); osw.flush(); osw.close(); } catch (Exception e) { Log.e(TAG, String.format("Failed to write log to [%s]", file), e); } return file; }
From source file:Main.java
public static synchronized void writeToFile(String rootPath, String filename, String data) { File file = new File(rootPath); if (!file.exists()) { file.mkdirs();//from w w w . j a va2s .c om } FileOutputStream fOut = null; try { File savedfile = new File(rootPath + filename); savedfile.createNewFile();//create file if not exists fOut = new FileOutputStream(savedfile, true); //append content to the end of file OutputStreamWriter outWriter = new OutputStreamWriter(fOut); outWriter.write(data); fOut.flush(); outWriter.flush(); fOut.close(); outWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:nz.co.lolnet.lolnetachievements.Achievements.Achievements.java
public static void awardPlayerAchievement(String playername, String achievementname) throws MalformedURLException, IOException, ParseException { JSONObject data = new JSONObject(); data.put("serverName", Config.SERVER_NAME); data.put("serverKey", Config.SERVER_HASH); data.put("achievementName", LolnetAchievements.convertAchievementName(achievementname)); data.put("playerName", playername.toLowerCase()); if (Config.DEBUG_MODE) { System.out.println(data.toJSONString()); }//ww w. j av a 2s. c om URL url = new URL("https://api-lnetachievements.rhcloud.com/api/achievements"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(API_TIMEOUT); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data.toJSONString()); wr.flush(); wr.close(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String temp = rd.readLine(); if (Config.DEBUG_MODE) { System.out.println(temp); } //JSONObject object = (JSONObject) new JSONParser().parse(temp); //System.out.println(""); rd.close(); }
From source file:com.fluidops.iwb.provider.GoogleRefineProvider.java
/** * utility to simplify doing POST requests * // www.j av a 2 s .c o m * @param url URL to post to * @param parameter map of parameters to post * @return URLConnection is a state where post parameters have been sent * @throws IOException * * TODO: move to util class */ public static URLConnection doPost(URL url, Map<String, String> parameter) throws IOException { URLConnection con = url.openConnection(); con.setDoOutput(true); StringBuilder params = new StringBuilder(); for (Entry<String, String> entry : parameter.entrySet()) params.append(StringUtil.urlEncode(entry.getKey())).append("=") .append(StringUtil.urlEncode(entry.getValue())).append("&"); // Send data OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); wr.write(params.toString()); wr.flush(); wr.close(); return con; }
From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java
public static byte[] compress(String string) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); OutputStreamWriter osw = new OutputStreamWriter(gzos); osw.write(string);/*from w w w . j ava 2s.c om*/ osw.flush(); osw.close(); return baos.toByteArray(); }
From source file:Main.java
/** * Method - saveMessage// w ww. j a v a 2 s . c o m * * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages * * @param ctx - Context of the Application that called the function * @param message - The messages to save to the file, most often a long String of several files * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively */ public static void saveMessage(Context ctx, String message, int mode) { try { // Creating objects to write to the file FileOutputStream fos = ctx.openFileOutput("messages.dat", mode); OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write(message); // Writing to the file osw.flush(); // Making sure all characters have been written // Closing objects after writing has finished osw.close(); fos.close(); } catch (FileNotFoundException e) { return; } catch (IOException e) { return; } }