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(new BufferedWriter(new FileWriter("myfile.txt", true))); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader }
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; } }
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); } }
public void dump(OutputStream o) { PrintWriter out = new PrintWriter(o, true); try { System.out.println("hi"); } catch (Throwable t) { t.printStackTrace(); } finally { if (o != System.out) { out.close(); } } } }
public static void save(ConfigurationManager cm, File cmLocation) { if (!cmLocation.getName().endsWith(CM_FILE_SUFFIX)) System.err.println("WARNING: Serialized s4-configuration should have the suffix '" + CM_FILE_SUFFIX + '\''); assert cm != null; try { PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cmLocation), Charset.forName("UTF-8"))); String configXML = ConfigurationManagerUtils.toXML(cm); pw.print(configXML); pw.flush(); pw.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } }
public void runCharniak(int n, String infile, String outfile, String errfile) { try { if (n == 1) n++; // Charniak does not output score if n = 1? List<String> args = new ArrayList<>(); args.add(parserExecutable); args.add(infile); ProcessBuilder process = new ProcessBuilder(args); process.directory(new File(this.dir)); PrintWriter out = IOUtils.getPrintWriter(outfile); PrintWriter err = IOUtils.getPrintWriter(errfile); SystemUtils.run(process, out, err); out.close(); err.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
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(); } } }
protected void store(String s) { try { PrintWriter writer = new PrintWriter(new FileWriter(getFilename())); writer.print(s); writer.close(); } catch (IOException e) { // do something with this error. e.printStackTrace(); } }
/** * 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 static void printCounter(TwoDimensionalCounter<String,String> cnt, String fname) { try { PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream(new File(fname)),false,"UTF-8")); for(String key : cnt.firstKeySet()) { for(String val : cnt.getCounter(key).keySet()) { pw.printf("%s\t%s\t%d%n", key, val, (int) cnt.getCount(key, val)); } } pw.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
@Override protected Void doInBackground() throws Exception { FileOutputStream fos = null; try { fos = new FileOutputStream(DENSITIES_PATHNAME); PrintWriter writer = new PrintWriter(fos); writer.write(mSavePayload); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("Couldn't save"); } return null; }
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(); } }
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); w.print(line); w.flush(); w.close();
public void produceTrees(String filename, int numTrees) { try { FileWriter fout = new FileWriter(filename); BufferedWriter bout = new BufferedWriter(fout); PrintWriter pout = new PrintWriter(bout); produceTrees(pout, numTrees); pout.close(); bout.close(); fout.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
/** * @param msg * @param e * @return string */ public static String toString(String msg, Throwable e) { UnsafeStringWriter w = new UnsafeStringWriter(); w.write(msg + "\n"); PrintWriter p = new PrintWriter(w); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }