Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8")); writer.write("Something"); } catch (IOException ex) { // report } finally { try {writer.close();} catch (Exception ex) {/*ignore*/} }
/** * Closes the underlying writer. * * @throws IOException if an error occurs closing the underlying writer */ @Override public void close() throws IOException { if (writer == null) { encoding = defaultEncoding; writer = new OutputStreamWriter(out, encoding); writer.write(xmlPrologWriter.toString()); } writer.close(); }
/** * Copy the contents of the given String to the given output Writer. * Closes the writer when done. * @param in the String to copy from * @param out the Writer to copy to * @throws IOException in case of I/O errors */ public static void copy(String in, Writer out) throws IOException { Assert.notNull(in, "No input String specified"); Assert.notNull(out, "No Writer specified"); try { out.write(in); } finally { try { out.close(); } catch (IOException ex) { } } }
/** * @param args output file path */ public static void main(final String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: <filepath>"); System.exit(0); } final File f = new File(args[0]); final Writer w = new FileWriter(f); w.write(XMLSchemaGenerator.xmlSchemaAsString(StandaloneCatalog.class)); w.close(); } }
@Test public void testExistingManifest() throws Exception { File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), TestJarFinder.class.getName() + "-testExistingManifest"); delete(dir); dir.mkdirs(); File metaInfDir = new File(dir, "META-INF"); metaInfDir.mkdirs(); File manifestFile = new File(metaInfDir, "MANIFEST.MF"); Manifest manifest = new Manifest(); OutputStream os = new FileOutputStream(manifestFile); manifest.write(os); os.close(); File propsFile = new File(dir, "props.properties"); Writer writer = new FileWriter(propsFile); new Properties().store(writer, ""); writer.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream zos = new JarOutputStream(baos); JarFinder.jarDir(dir, "", zos); JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); Assert.assertNotNull(jis.getManifest()); jis.close(); }
private File purgeConfig(String fileName) throws IOException { File configFile = folder.newFile(fileName); Writer streamWriter = new OutputStreamWriter(new FileOutputStream(configFile), Charset.forName("UTF-8")); PrintWriter printWriter = new PrintWriter(streamWriter); printWriter.println("purge: 1"); printWriter.println("include:"); printWriter.println(" - '**/" + YAML_TEST_1 + "'"); streamWriter.close(); printWriter.close(); return configFile; }
public static void main(String[] args) throws Exception { String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Table.docx"; //String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Word2007-fonts.docx"; WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement(); Writer out = new OutputStreamWriter(System.out); extractText(wmlDocumentEl, out); //out.flush(); out.close(); }
File dir = new File(COMPILE_DIR); if (packageName != null) { dir = new File(dir, packageName.replace('.', '/')); FileUtils.createDirectories(dir.getAbsolutePath()); File javaFile = new File(dir, className + ".java"); File classFile = new File(dir, className + ".class"); try { Writer out = IOUtils.getBufferedWriter(f); classFile.delete(); out.write(source); out.close(); if (JAVAC_SUN != null) { javacSun(javaFile);
private void write(Serializer serializer, String path, SerializerConfig serializerConfig, EntityType type) throws IOException { File targetFile = new File(targetFolder, path); generatedFiles.add(targetFile); Writer w = writerFor(targetFile); try { CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w); serializer.serialize(type, serializerConfig, writer); } finally { w.close(); } }
@Test public void testNoManifest() throws Exception { File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), TestJarFinder.class.getName() + "-testNoManifest"); delete(dir); dir.mkdirs(); File propsFile = new File(dir, "props.properties"); Writer writer = new FileWriter(propsFile); new Properties().store(writer, ""); writer.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream zos = new JarOutputStream(baos); JarFinder.jarDir(dir, "", zos); JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); Assert.assertNotNull(jis.getManifest()); jis.close(); } }
fileWriter = new FileWriter( to ); FileOutputStream outstream = new FileOutputStream( to ); fileWriter = new OutputStreamWriter( outstream, encoding ); fileWriter.close(); fileWriter = null; fileReader.close();
public void process(OutputStream out) throws IOException { Writer w = new OutputStreamWriter(out, charset); c.call(w); w.flush(); w.close(); } });
Writer scriptWriter = null; try { completionWriter = new FileWriter(out); completionWriter.write(autoCompleteScript); scriptWriter = new FileWriter(command); scriptWriter.write("" + "#!/usr/bin/env bash\n" + "\n" + if (completionWriter != null) { completionWriter.close(); } if (scriptWriter != null) { scriptWriter.close(); }
private void generate(Template template, File outDirFile, String javaPackage, String javaClassName, Schema schema, Entity entity, Map<String, Object> additionalObjectsForTemplate) throws Exception { Map<String, Object> root = new HashMap<>(); root.put("schema", schema); root.put("entity", entity); if (additionalObjectsForTemplate != null) { root.putAll(additionalObjectsForTemplate); } try { File file = toJavaFilename(outDirFile, javaPackage, javaClassName); //noinspection ResultOfMethodCallIgnored file.getParentFile().mkdirs(); if (entity != null && entity.getHasKeepSections()) { checkKeepSections(file, root); } Writer writer = new FileWriter(file); try { template.process(root, writer); writer.flush(); System.out.println("Written " + file.getCanonicalPath()); } finally { writer.close(); } } catch (Exception ex) { System.err.println("Data map for template: " + root); System.err.println("Error while generating " + javaPackage + "." + javaClassName + " (" + outDirFile.getCanonicalPath() + ")"); throw ex; } }
Writer w = new FileWriter(...); try { // write stuff to the file } finally { try { w.close(); } catch (IOException ex) { // Log error writing file and bail out. } }
Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("outfilename"), "UTF-8")); try { out.write(aString); } finally { out.close(); }
/** * @param args output file path */ public static void main(final String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: <filepath>"); System.exit(0); } final File f = new File(args[0]); final Writer w = new FileWriter(f); w.write(XMLSchemaGenerator.xmlSchemaAsString(DefaultOverdueConfig.class)); w.close(); } }
private File includeOptionConfig(String fileName) throws IOException { File configFile = folder.newFile(fileName); Writer streamWriter = new OutputStreamWriter(new FileOutputStream(configFile), Charset.forName("UTF-8")); PrintWriter printWriter = new PrintWriter(streamWriter); printWriter.println("include:"); printWriter.println(" - '**/" + YAML_TEST_1 + "'"); streamWriter.close(); printWriter.close(); return configFile; }