/** * write lines. * * @param os output stream. * @param lines lines. * @throws IOException */ public static void writeLines(OutputStream os, String[] lines) throws IOException { PrintWriter writer = new PrintWriter(new OutputStreamWriter(os)); try { for (String line : lines) { writer.println(line); } writer.flush(); } finally { writer.close(); } }
private static String formatThrowable(Throwable t) { StringWriter buf = new StringWriter(); PrintWriter writer = new PrintWriter(buf); t.printStackTrace(); writer.flush(); return buf.toString(); }
/** * Writes out data from this Object. * @param w Data is written to this Writer */ public void writeData(Writer w) { PrintWriter out = new PrintWriter(w); // all lines have one rule per line for (UnaryRule ur : this) { out.println(ur.toString(index)); } out.flush(); }
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); ) { writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF).append(param).append(CRLF).flush(); writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset! writer.append(CRLF).flush(); Files.copy(textFile.toPath(), output); writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); Files.copy(binaryFile.toPath(), output); writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. writer.append("--" + boundary + "--").append(CRLF).flush();
public void write(String txt, OutputStream out) { PrintWriter printer = new PrintWriter(out); printer.print(txt); printer.flush(); //it is very unpolite to close someone else's streams! //printer.close(); }
@Override public void dump(OutputStream output) { // Do not close this writer in order to keep output stream open. final PrintWriter p = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)); p.printf("Dump of %s:%n", this); for (int i = 0; i < values.size(); i++) { p.printf("<%d> %s%n", i, values.get(i)); } p.flush(); } }
public void dumpOptimizerPlanAsJSON(OptimizedPlan plan, File toFile) throws IOException { PrintWriter pw = null; try { pw = new PrintWriter(new FileOutputStream(toFile), false); dumpOptimizerPlanAsJSON(plan, pw); pw.flush(); } finally { if (pw != null) { pw.close(); } } }
private static String buildStackTrace(Throwable t, boolean toHtml, StackTraceType type) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); pw.flush(); String stackTrace = sw.getBuffer().toString(); if (type == StackTraceType.SHORT && !isTooVerbose()) { stackTrace = filterTrace(sw.getBuffer().toString()); } if (toHtml) { stackTrace = escapeHtml(stackTrace); } return stackTrace; }
@Test public void nestedRuntimeExceptionWithNoRootCause() { String mesg = "mesg of mine"; // Making a class abstract doesn't _really_ prevent instantiation :-) NestedRuntimeException nex = new NestedRuntimeException(mesg) {}; assertNull(nex.getCause()); assertEquals(nex.getMessage(), mesg); // Check printStackTrace ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(baos); nex.printStackTrace(pw); pw.flush(); String stackTrace = new String(baos.toByteArray()); assertTrue(stackTrace.contains(mesg)); }
/** * write lines. * * @param os output stream. * @param lines lines. * @throws IOException */ public static void writeLines(OutputStream os, String[] lines) throws IOException { PrintWriter writer = new PrintWriter(new OutputStreamWriter(os)); try { for (String line : lines) { writer.println(line); } writer.flush(); } finally { writer.close(); } }
/** * Prints full exception stack trace, from top to root cause, into a String. */ public static String exceptionChainToString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); while (t != null) { t.printStackTrace(pw); t = t.getCause(); } pw.flush(); sw.flush(); return sw.toString(); }
public void saveToFile(final String text, final Path destPath) throws IOException { try (PrintWriter out = new PrintWriter( Files.newBufferedWriter(destPath, StandardCharsets.UTF_8))) { out.println(text); out.flush(); } }
@Test public void testSpanningChild() { long basicCuboid = getBaseCuboid(10); List<Long> cuboids = genRandomCuboids(basicCuboid, 50); long testCuboid = cuboids.get(10); System.out.println(cuboids); CuboidTree cuboidTree = CuboidTree.createFromCuboids(cuboids); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)); cuboidTree.print(out); out.flush(); List<Long> spanningChildren = cuboidTree.getSpanningCuboid(testCuboid); System.out.println(testCuboid + ":" + spanningChildren); }
public void writeData(Writer w) {//throws IOException { PrintWriter out = new PrintWriter(w); StringBuilder sb = new StringBuilder(); sb.append(lexOptions.toString()); sb.append("parserParams ").append(tlpParams.getClass().getName()).append("\n"); sb.append("forceCNF ").append(forceCNF).append("\n"); sb.append("doPCFG ").append(doPCFG).append("\n"); sb.append("doDep ").append(doDep).append("\n"); sb.append("freeDependencies ").append(freeDependencies).append("\n"); sb.append("directional ").append(directional).append("\n"); sb.append("genStop ").append(genStop).append("\n"); sb.append("distance ").append(distance).append("\n"); sb.append("coarseDistance ").append(coarseDistance).append("\n"); sb.append("dcTags ").append(dcTags).append("\n"); sb.append("nPrune ").append(nodePrune).append("\n"); out.print(sb.toString()); out.flush(); }
public void dump(OutputStream output) { // Do not close this writer in order to keep output stream open. final PrintWriter p = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)); p.printf("Dump of %s:%n", this); dump(p, root, 0); p.flush(); }