List of usage examples for java.io Writer write
public void write(String str) throws IOException
From source file:com.github.jknack.handlebars.server.HbsServer.java
/** * Start a Handlebars server.//from w w w . j av a 2s . com * * @param args The command line arguments. * @throws Exception If something goes wrong. */ public static void run(final Options args) throws Exception { if (!args.dir.exists()) { System.out.println("File not found: " + args.dir); } logger.info("Welcome to the Handlebars.java server v" + version); URLTemplateLoader loader = new FileTemplateLoader(args.dir); loader.setPrefix(new File(args.dir, args.prefix).getAbsolutePath()); loader.setSuffix(args.suffix); Handlebars handlebars = new Handlebars(loader); /** * Helper wont work in the stand-alone version, so we add a default helper * that render the plain text. */ handlebars.registerHelper(HelperRegistry.HELPER_MISSING, new Helper<Object>() { @Override public CharSequence apply(final Object context, final com.github.jknack.handlebars.Options options) throws IOException { return new Handlebars.SafeString(options.fn.text()); } }); handlebars.registerHelper("json", Jackson2Helper.INSTANCE); handlebars.registerHelper("md", new MarkdownHelper()); // String helpers StringHelpers.register(handlebars); // Humanize helpers HumanizeHelper.register(handlebars); final Server server = new Server(args.port); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { logger.info("Hope you enjoy it! bye!"); try { server.stop(); } catch (Exception ex) { logger.info("Cann't stop the server", ex); } } })); server.addLifeCycleListener(new AbstractLifeCycleListener() { @Override public void lifeCycleStarted(final LifeCycle event) { logger.info("Open a browser and type:"); logger.info(" http://localhost:{}{}/[page]{}", new Object[] { args.port, args.contextPath.equals(CONTEXT) ? "" : args.contextPath, args.suffix }); } }); WebAppContext root = new WebAppContext(); ErrorHandler errorHandler = new ErrorHandler() { @Override protected void writeErrorPageHead(final HttpServletRequest request, final Writer writer, final int code, final String message) throws IOException { writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>\n"); writer.write("<title>{{"); writer.write(Integer.toString(code)); writer.write("}}"); writer.write("</title>\n"); writer.write("<style>body{font-family: monospace;}</style>"); } @Override protected void writeErrorPageMessage(final HttpServletRequest request, final Writer writer, final int code, final String message, final String uri) throws IOException { writer.write("<div align=\"center\">"); writer.write( "<p><span style=\"font-size: 48px;\">{{</span><span style=\"font-size: 36px; color:#999;\">"); writer.write(Integer.toString(code)); writer.write("</span><span style=\"font-size: 48px;\">}}</span></p>"); writer.write("</h2>\n<p>Problem accessing "); write(writer, uri); writer.write(". Reason:\n<pre> "); write(writer, message); writer.write("</pre></p>"); writer.write("</div>"); writer.write("<hr />"); } @Override protected void writeErrorPageBody(final HttpServletRequest request, final Writer writer, final int code, final String message, final boolean showStacks) throws IOException { String uri = request.getRequestURI(); writeErrorPageMessage(request, writer, code, message, uri); } }; root.setErrorHandler(errorHandler); root.setContextPath(args.contextPath); root.setResourceBase(args.dir.getAbsolutePath()); root.addServlet(new ServletHolder(new HbsServlet(handlebars, args)), "*" + args.suffix); root.setParentLoaderPriority(true); // prevent jetty from loading the webapp web.xml root.setConfigurations(new Configuration[] { new WebXmlConfiguration() { @Override protected Resource findWebXml(final WebAppContext context) throws IOException, MalformedURLException { return null; } } }); server.setHandler(root); server.start(); server.join(); }
From source file:juicebox.tools.utils.juicer.apa.APAUtils.java
/** * @param filename//from w ww.j av a 2s. c o m * @param matrix */ public static void saveMeasures(String filename, RealMatrix matrix) { Writer writer = null; APARegionStatistics apaStats = new APARegionStatistics(matrix); try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8")); writer.write("P2M" + '\t' + apaStats.getPeak2mean() + '\n'); writer.write("P2UL" + '\t' + apaStats.getPeak2UL() + '\n'); writer.write("P2UR" + '\t' + apaStats.getPeak2UR() + '\n'); writer.write("P2LL" + '\t' + apaStats.getPeak2LL() + '\n'); writer.write("P2LR" + '\t' + apaStats.getPeak2LR() + '\n'); writer.write("ZscoreLL" + '\t' + apaStats.getZscoreLL()); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (writer != null) writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java
public static void saveHistory(RuntimeBean runtime, File source) throws IOException { JSONArray transs = ToJSONLogic.toJSONTransactions(runtime.getHistory()); OutputStream os = new FileOutputStream(source); Writer wtr = new OutputStreamWriter(os, "utf-8"); wtr.write(transs.toJSONString()); wtr.close();/*from w ww .j av a 2 s .c o m*/ setProp("app.history", source.toString()); }
From source file:com.all.login.services.LoginDatabaseAccess.java
private static void save(File file, LoginDatabase db) { Writer out = null; try {/*from w ww.j av a 2 s . co m*/ out = new OutputStreamWriter(new FileOutputStream(file)); out.write(JsonConverter.toJson(db)); } catch (Exception e) { LOG.error("Could not serialize LoginDatabase.", e); } finally { try { out.close(); } catch (Exception e) { LOG.error("Could not close LoginDatabase output stream.", e); } } }
From source file:org.crazydog.util.spring.FileCopyUtils.java
/** * Copy the contents of the given String to the given output Writer. * Closes the writer when done.//from w ww.j a v a 2s . co m * @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 { org.springframework.util.Assert.notNull(in, "No input String specified"); org.springframework.util.Assert.notNull(out, "No Writer specified"); try { out.write(in); } finally { try { out.close(); } catch (IOException ex) { } } }
From source file:org.jboss.as.test.integration.security.loginmodules.common.Utils.java
public static synchronized void logToFile(String what, String where) { try {/*from w w w . j av a 2 s .co m*/ File file = new File(where); if (!file.exists()) { file.createNewFile(); } OutputStream os = new FileOutputStream(file, true); Writer writer = new OutputStreamWriter(os); writer.write(what); if (!what.endsWith("\n")) { writer.write("\n"); } writer.close(); os.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:com.ms.commons.test.classloader.util.CLFileUtil.java
public static void copyAndProcessFileContext(URL url, File newFile, String encoding, Processor processor) { try {/*from www . java2s . co m*/ if (processor == null) { processor = DEFAULT_PROCESSOR; } if (!newFile.exists()) { String fileContext = readUrlToString(url, null); if (encoding == null) { if (FileUtil.convertURLToFilePath(url).endsWith(".xml") && fileContext.contains("\"UTF-8\"")) { encoding = "UTF-8"; fileContext = readUrlToString(url, "UTF-8"); } } (new File(getFilePath(newFile.getPath()))).mkdirs(); Writer writer; if (encoding == null) { writer = new OutputStreamWriter(new FileOutputStream(newFile)); } else { writer = new OutputStreamWriter(new FileOutputStream(newFile), encoding); } try { writer.write(processor.process(fileContext)); writer.flush(); } finally { writer.close(); } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.googlecode.jsonplugin.JSONUtil.java
/** * Serializes an object into JSON to the given writer. * * @param writer Writer to serialize the object to * @param object object to be serialized * @throws IOException//from w w w. j a va 2 s.c o m * @throws JSONException */ public static void serialize(Writer writer, Object object) throws IOException, JSONException { writer.write(serialize(object)); }
From source file:com.tomgibara.cluster.CreateUniformCircles.java
private static void writeCluster(NormalizedRandomGenerator gen, double[] means, double[] deviations, int size, Writer writer) throws IOException { UncorrelatedRandomVectorGenerator c = new UncorrelatedRandomVectorGenerator(means, deviations, gen); int count = 0; while (count < size) { double[] pt = c.nextVector(); double x = pt[0] - means[0]; double y = pt[1] - means[1]; if (x * x + y * y > deviations[0] * deviations[1]) continue; writer.write(String.format("%3.3f %3.3f%n", pt[0], pt[1])); count++;// www .j a v a2 s .c om } }
From source file:com.sun.faban.harness.webclient.Deployer.java
private static void writeTrailer(Writer w) throws IOException { w.write(" </b></center>"); w.write(" </body>\n"); w.write("</html>\n"); }