canonical example by Tabnine
public void writeToFile(File dest, String content, boolean append) throws IOException { // append - true for writing to the end of the file rather to the beginning try (PrintWriter writer = new PrintWriter(new FileWriter(dest, append))) { writer.print(content); } }
StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); sw.toString(); // stack trace as a string
try{ PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8"); writer.println("The first line"); writer.println("The second line"); writer.close(); } catch (IOException e) { // do something }
try( PrintWriter out = new PrintWriter( "filename.txt" ) ){ out.println( text ); }
PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true))); out.println("the text"); }catch (IOException e) { System.err.println(e); }finally{ if(out != null){ out.close(); } }
public static String toString(Throwable e) { StringWriter w = new StringWriter(); PrintWriter p = new PrintWriter(w); p.print(e.getClass().getName() + ": "); if (e.getMessage() != null) { p.print(e.getMessage() + "\n"); } p.println(); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }
private void save() { try { PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(path)), "UTF-8")); urlTokens.forEach(printWriter::println); printWriter.close(); } catch (IOException e) { logger.error("write file error", e); } }
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) { out.println("the text"); }catch (IOException e) { System.err.println(e); }
private static void createTupleClasses(File root) throws FileNotFoundException { File dir = getPackage(root, PACKAGE); for (int i = FIRST; i <= LAST; i++) { File tupleFile = new File(dir, "Tuple" + i + ".java"); PrintWriter writer = new PrintWriter(tupleFile); writeTupleClass(writer, i); writer.flush(); writer.close(); } }
private void persistIndexMap(IndexEntry entry) throws IOException { File mapFile = new File(segmentDirectory, INDEX_MAP_FILE); try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(mapFile, true)))) { String startKey = getKey(entry.key.name, entry.key.type.getIndexName(), true); StringBuilder sb = new StringBuilder(); sb.append(startKey).append(" = ").append(entry.startOffset); writer.println(sb.toString()); String endKey = getKey(entry.key.name, entry.key.type.getIndexName(), false); sb = new StringBuilder(); sb.append(endKey).append(" = ").append(entry.size); writer.println(sb.toString()); } }
File textFile = new File("/path/to/file.txt"); File binaryFile = new File("/path/to/file.bin"); 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.
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(); } }
/** * 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(); } }
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(); } } }
@Override public void handle(MvcResult result) throws Exception { if (logger.isDebugEnabled()) { StringWriter stringWriter = new StringWriter(); ResultHandler printingResultHandler = new PrintWriterPrintingResultHandler(new PrintWriter(stringWriter)); printingResultHandler.handle(result); logger.debug("MvcResult details:\n" + stringWriter); } } }
@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)); }
public static void itemsToFile(OutputStream os, List<String> items) { try { PrintWriter printWriter = new PrintWriter(os); items.forEach(printWriter::println); printWriter.close(); } catch (Exception e) { throw e; } }
void dumpLedgers(Set<Long> ledgers, File targetFile) throws Exception { PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(targetFile), UTF_8.name())); try { for (Long ledger : ledgers) { pw.println(ledger); } } finally { pw.close(); } System.out.println("Dump " + ledgers.size() + " ledgers to file : " + targetFile); }
public static String toString(Throwable e) { StringWriter w = new StringWriter(); PrintWriter p = new PrintWriter(w); p.print(e.getClass().getName() + ": "); if (e.getMessage() != null) { p.print(e.getMessage() + "\n"); } p.println(); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }
public static String getStackTrace(Throwable ex) { StringWriter buf = new StringWriter(); ex.printStackTrace(new PrintWriter(buf)); return buf.toString(); }