List of usage examples for java.io Writer close
public abstract void close() throws IOException;
From source file:com.hmsinc.epicenter.webapp.map.StyleBuilder.java
void build(OutputStream stream, StyleParameters styleParameters) throws IOException { final Map<String, Object> template = createTemplate( featureGrader.gradeFeatures(getProbabilities(styleParameters))); template.put("render_labels", styleParameters.isLabelFeatures()); template.put("geography", styleParameters.getLayerName()); final Writer writer = new BufferedWriter(new OutputStreamWriter(stream)); VelocityEngineUtils.mergeTemplate(velocityEngine, TEMPLATE_NAME, template, writer); writer.close(); }
From source file:com.johnson3yo.dubem.plugin.ArtifactWizardMojo.java
private void updateAppServletXml(String functionName) throws IOException, JDOMException { String[] cmd = { "/bin/sh", "-c", "cd ../../pegasus-bundle/foundation-guiwar/src/main/webapp/WEB-INF/spring; pwd" }; Process p = Runtime.getRuntime().exec(cmd); String output = IOUtils.toString(p.getInputStream()); File f = new File(output.trim() + "/app-servlet.xml"); SAXBuilder builder = new SAXBuilder(); Document document = (Document) builder.build(f); Element rootNode = document.getRootElement(); List<Element> list = rootNode.getChildren("component-scan", Namespace.getNamespace("http://www.springframework.org/schema/context")); Element e = list.get(0);//from w w w . java 2s . c o m String cnt = "<context:exclude-filter type=\"regex\" expression=\"pegasus\\.module\\." + functionName.toLowerCase() + "\\..*\" />"; e.addContent(cnt); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()) { @Override public String escapeElementEntities(String str) { return str; } }; Writer writer = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); outputter.output(document, writer); writer.close(); }
From source file:com.netxforge.oss2.config.NotificationFactory.java
/** {@inheritDoc} */ protected void saveXML(String xmlString) throws IOException { if (xmlString != null) { Writer fileWriter = new OutputStreamWriter(new FileOutputStream(m_noticeConfFile), "UTF-8"); fileWriter.write(xmlString);/* ww w . j av a2 s. c o m*/ fileWriter.flush(); fileWriter.close(); } }
From source file:com.streamsets.pipeline.stage.origin.spooldir.TestTextSpoolDirSource.java
@Test public void testGbkEncodedFile() throws Exception { SpoolDirSource source = createSource("GBK"); SourceRunner runner = new SourceRunner.Builder(SpoolDirDSource.class, source).addOutputLane("lane").build(); runner.runInit();//w w w.jav a 2s .c o m try { // Write out a gbk-encoded file. File f = new File(createTestDir(), "test_gbk.log"); Writer writer = new OutputStreamWriter(new FileOutputStream(f), "GBK"); IOUtils.write(UTF_STRING, writer); writer.close(); // Read back the file to verify its content is gbk-encoded. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF8")); Assert.assertEquals(GBK_STRING, reader.readLine()); reader.close(); BatchMaker batchMaker = SourceRunner.createTestBatchMaker("lane"); Assert.assertEquals("-1", source.produce(f, "0", 10, batchMaker)); StageRunner.Output output = SourceRunner.getOutput(batchMaker); List<Record> records = output.getRecords().get("lane"); Assert.assertNotNull(records); Assert.assertEquals(1, records.size()); Assert.assertEquals(UTF_STRING.substring(0, 10), records.get(0).get().getValueAsMap().get("text").getValueAsString()); Assert.assertTrue(records.get(0).has("/truncated")); } finally { runner.runDestroy(); } }
From source file:com.wavemaker.tools.project.upgrade.swamis.ServiceBeanFileUpgrade.java
@Override public void doUpgrade(Project project, UpgradeInfo upgradeInfo) { DesignServiceManager dsm = DesignTimeUtils.getDesignServiceManager(project); List<String> touchedProjects = new ArrayList<String>(); for (Service service : dsm.getServices()) { if (service.getSpringFile() == null) { // create the service bean file File serviceBeanFile = dsm.getServiceBeanXmlFile(service.getId()); if (!serviceBeanFile.exists()) { try { DesignServiceManager.generateSpringServiceConfig(service.getId(), service.getClazz(), dsm.getDesignServiceType(service.getType()), serviceBeanFile, project); } catch (JAXBException e) { throw new WMRuntimeException(e); } catch (IOException e) { throw new WMRuntimeException(e); }//from www. j a va 2 s . c om } // edit the servicedef File serviceDefFile = dsm.getServiceDefXmlFile(service.getId()); service.setSpringFile(serviceBeanFile.getName()); Marshaller marshaller; try { marshaller = definitionsContext.createMarshaller(); marshaller.setProperty("jaxb.formatted.output", true); Writer writer = serviceDefFile.getContent().asWriter(); try { marshaller.marshal(service, writer); } finally { writer.close(); } } catch (Exception e) { throw new WMRuntimeException(e); } // finally, add this to the list of modified services touchedProjects.add(service.getId()); } } if (!touchedProjects.isEmpty()) { upgradeInfo.addVerbose("Converted bean definitions to a separate file for services: " + StringUtils.join(touchedProjects, ", ")); } }
From source file:com.joliciel.frenchTreebank.export.FrenchTreebankXmlWriter.java
public void write(File outDir) throws TemplateException, IOException { List<TreebankFile> treebankFiles = treebankService.findTreebankFiles(); for (TreebankFile treebankFile : treebankFiles) { String ftbFileName = treebankFile.getFileName(); String fileName = ftbFileName.substring(ftbFileName.lastIndexOf('/') + 1); File xmlFile = new File(outDir, fileName); xmlFile.delete();/*from w w w . java2 s.c om*/ xmlFile.createNewFile(); Writer xmlFileWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(xmlFile, false), "UTF8")); this.write(xmlFileWriter, treebankFile); xmlFileWriter.flush(); xmlFileWriter.close(); } }
From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java
private static long getStringSizeInBytes(String str) { CountingOutputStream counterOutputStream = new CountingOutputStream(); try {/* www . jav a 2s. co m*/ Writer writer = new OutputStreamWriter(counterOutputStream, "UTF-8"); writer.write(str); writer.flush(); writer.close(); } catch (IOException e) { String errorMessage = "Failed to calculate the size of message payload."; LOG.error(errorMessage, e); throw new AmazonClientException(errorMessage, e); } return counterOutputStream.getTotalSize(); }
From source file:com.android.talkback.labeling.CustomLabelMigrationManager.java
private void writeToFile(String content, File file) throws IOException { Writer writer = new BufferedWriter(new FileWriter(file)); writer.write(content);/* ww w .java 2 s . c o m*/ writer.close(); }
From source file:com.eufar.asmm.server.DownloadFunction.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("DownloadFunction - the function started"); ServletContext context = getServletConfig().getServletContext(); request.setCharacterEncoding("UTF-8"); String dir = context.getRealPath("/tmp"); ;/*from ww w .j a v a 2s . c o m*/ String filename = ""; File fileDir = new File(dir); try { System.out.println("DownloadFunction - create the file on server"); filename = request.getParameterValues("filename")[0]; String xmltree = request.getParameterValues("xmltree")[0]; // format xml code to pretty xml code Document doc = DocumentHelper.parseText(xmltree); StringWriter sw = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setIndent(true); format.setIndentSize(4); XMLWriter xw = new XMLWriter(sw, format); xw.write(doc); Writer out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(dir + "/" + filename), "UTF-8")); out.append(sw.toString()); out.flush(); out.close(); } catch (Exception ex) { System.out.println("ERROR during rendering: " + ex); } try { System.out.println("DownloadFunction - send file to user"); ServletOutputStream out = response.getOutputStream(); File file = new File(dir + "/" + filename); String mimetype = context.getMimeType(filename); response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setHeader("Pragma", "private"); response.setHeader("Cache-Control", "private, must-revalidate"); DataInputStream in = new DataInputStream(new FileInputStream(file)); int length; while ((in != null) && ((length = in.read(bbuf)) != -1)) { out.write(bbuf, 0, length); } in.close(); out.flush(); out.close(); FileUtils.cleanDirectory(fileDir); } catch (Exception ex) { System.out.println("ERROR during downloading: " + ex); } System.out.println("DownloadFunction - file ready to be donwloaded"); }
From source file:com.netxforge.oss2.config.ThreshdConfigFactory.java
/** {@inheritDoc} */ protected void saveXML(String xmlString) throws IOException { File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.THRESHD_CONFIG_FILE_NAME); Writer fileWriter = new OutputStreamWriter(new FileOutputStream(cfgFile), "UTF-8"); fileWriter.write(xmlString);/*from w ww. j a v a 2 s . c o m*/ fileWriter.flush(); fileWriter.close(); }