List of usage examples for java.io OutputStreamWriter OutputStreamWriter
public OutputStreamWriter(OutputStream out, CharsetEncoder enc)
From source file:com.ms.commons.test.treedb.Objects2XmlFileUtil.java
public static void write(String testMethodName, BuiltInCacheKey prePareOrResult, List<?>... prepareData) { OutputStreamWriter writer = null; File file = getFile(testMethodName, prePareOrResult); try {//from w w w. ja va 2 s. c o m writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); XStream xStream = new XStream(); StringBuilder root = new StringBuilder(); root.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); root.append("<").append(ROOT).append(">"); for (List<?> objectList : prepareData) { if (objectList == null || objectList.size() <= 0) continue; StringBuilder objectStr = objectList2XmlString(xStream, objectList); root.append(objectStr); } root.append("</").append(ROOT).append(">"); writer.write(prettyPrint(root.toString())); writer.close(); } catch (FileNotFoundException e) { throw new RuntimeException("write " + file.getAbsolutePath() + " failed", e); } catch (IOException e) { throw new RuntimeException("write " + file.getAbsolutePath() + " failed", e); } finally { close(writer); } }
From source file:Main.java
/** * Uses http to post an XML String to a URL. * * @param url/*from w w w . ja v a 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.netflix.config.DynamicFileConfigurationTest.java
static File createConfigFile(String prefix) throws Exception { configFile = File.createTempFile(prefix, ".properties"); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8")); writer.write("dprops1=123456789"); writer.newLine();//from w w w. j av a 2 s .co m writer.write("dprops2=79.98"); writer.newLine(); writer.close(); System.err.println(configFile.getPath() + " created"); return configFile; }
From source file:Main.java
/** * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out). * * @param _docOrNode {@link Document} or {@link Node} object * @param _outStream {@link OutputStream} to print on * @throws IOException on error/*from w ww . j a v a 2 s. c o m*/ */ public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException { if (_docOrNode == null || _outStream == null) { throw new IOException("Cannot print (on) 'null' object"); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(_docOrNode), new StreamResult(new OutputStreamWriter(_outStream, "UTF-8"))); } catch (UnsupportedEncodingException | TransformerException _ex) { throw new IOException("Could not print Document or Node.", _ex); } }
From source file:com.daphne.es.maintain.staticresource.web.controller.utils.YuiCompressorUtils.java
/** * js/css ??.min.js/css/*w w w . j ava 2s. c om*/ * @param fileName * @return ?? */ public static String compress(final String fileName) { String minFileName = null; final String extension = FilenameUtils.getExtension(fileName); if ("js".equalsIgnoreCase(extension)) { minFileName = fileName.substring(0, fileName.length() - 3) + ".min.js"; } else if ("css".equals(extension)) { minFileName = fileName.substring(0, fileName.length() - 4) + ".min.css"; } else { throw new RuntimeException( "file type only is js/css, but was fileName:" + fileName + ", extension:" + extension); } Reader in = null; Writer out = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), Constants.ENCODING)); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(minFileName), Constants.ENCODING)); if ("js".equals(extension)) { CustomErrorReporter errorReporter = new CustomErrorReporter(); JavaScriptCompressor compressor = new JavaScriptCompressor(in, errorReporter); compressor.compress(out, 10, true, false, false, false); if (errorReporter.hasError()) { throw new RuntimeException(errorReporter.getErrorMessage()); } } else if ("css".equals(extension)) { CssCompressor compressor = new CssCompressor(in); compressor.compress(out, 10); } } catch (Exception e) { //?js/css try { FileUtils.copyFile(new File(fileName), new File(minFileName)); } catch (IOException ioException) { throw new RuntimeException("compress error:" + ioException.getMessage(), ioException); } throw new RuntimeException("compress error:" + e.getMessage(), e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } if (FileUtils.sizeOf(new File(minFileName)) == 0) { try { FileUtils.copyFile(new File(fileName), new File(minFileName)); } catch (IOException ioException) { throw new RuntimeException("compress error:" + ioException.getMessage(), ioException); } } return minFileName; }
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 {// www .j av a 2 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:idp.file_operator.java
public void write_csv(String[] data) { try {//from w w w . j a v a2 s . com CSVWriter writer = new CSVWriter( new OutputStreamWriter(new FileOutputStream(path + data[0] + ".csv", true), "UTF-8"), ',', CSVWriter.NO_QUOTE_CHARACTER); writer.writeNext(data); writer.close(); } catch (IOException ex) { } }
From source file:Main.java
public static void saveDomSource(final DOMSource source, final File target, final File xslt) throws TransformerException, IOException { TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer trans;/*from w ww. j a v a2s. c o m*/ if (xslt == null) { trans = transFact.newTransformer(); } else { trans = transFact.newTransformer(new StreamSource(xslt)); } trans.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(target); trans.transform(source, new StreamResult(new OutputStreamWriter(fos, "UTF-8"))); fos.close(); }
From source file:Main.java
/** * //w ww . j av a2 s. c om * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ public static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }
From source file:cat.tv3.eng.rec.recomana.lupa.visualization.TextsResumeToJson.java
private static void saveResults(JSONArray recomendations, String id) { Writer out;//from w ww. j a va2s . c om try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("data_toVisualize/data_resume/resume_" + id + ".json"), "UTF-8")); try { out.write(recomendations.toJSONString()); out.close(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }