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(FileWriter fw = new FileWriter("outfilename", true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println("the text"); //more code out.println("more text"); //more code } catch (IOException e) { //exception handling left as an exercise for the reader }
/** * 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(); } }
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(); } }
private static void saveIndex() { assert remote2local != null; File downloadDir = new File(System.getProperty("mary.downloadDir", ".")); File licenseIndexFile = new File(downloadDir, "license-index.txt"); try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(licenseIndexFile), "UTF-8"))) { for (URL remote : remote2local.keySet()) { pw.println(remote2local.get(remote) + "|" + remote.toString()); } } catch (IOException e) { System.err.println("Problem updating the index file " + licenseIndexFile.getAbsolutePath()); e.printStackTrace(); } }
String outputFullFilename = new File(outputDirectory).getAbsolutePath() + Constant.Symbol.SLASH_LEFT + (packageName.replace(Constant.Symbol.DOT, Constant.Symbol.SLASH_LEFT) + Constant.Symbol.SLASH_LEFT + "R" + Constant.Symbol.DOT + Constant.File.JAVA); FileUtil.createFile(outputFullFilename); PrintWriter writer = null; try { writer = new PrintWriter(new FileOutputStream(outputFullFilename)); writer.format("package %s;\n\n", packageName); writer.println("public final class R {\n"); for (RType rType : rTypeResourceMap.keySet()) { writer.println(" }\n"); writer.println("}"); } catch (Exception e) { throw new AaptUtilException(e);
private static void reportError(String yytext) { try { PrintWriter p = new PrintWriter(new OutputStreamWriter(System.err, "utf-8"), true); p.println("chtbl.flex tokenization error: \"" + yytext + "\""); if (yytext.length() >= 1) { p.println("First character is: " + yytext.charAt(0)); if (yytext.length() >= 2) { p.println("Second character is: " + yytext.charAt(1)); } } } catch (UnsupportedEncodingException e) { System.err.println("chtbl.flex tokenization and encoding present error"); } }
public static void test(String[] args) throws IOException { InputStream is = new FileInputStream("test.txt"); OutputStream os = new FileOutputStream("/tmp/test.txt"); try { BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); String line; while ((line = r.readLine()) != null) { w.println(line); } } finally { is.close(); } }
private static void exportDiagramErrorText(OutputStream os, Throwable exception, List<String> strings) { final PrintWriter pw = new PrintWriter(os); exception.printStackTrace(pw); pw.println(); pw.println(); for (String s : strings) { s = s.replaceAll("\\</?\\w+?\\>", ""); pw.println(s); } pw.flush(); }
protected void sendOkResponseWithContent(Request request, HttpServletResponse response, Applications apps) throws IOException { String content = XmlXStream.getInstance().toXML(apps); response.setContentType("application/xml"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println(content); response.getWriter().flush(); request.setHandled(true); System.out.println("Eureka port: " + port + ". " + System.currentTimeMillis() + ". Eureka resource mock, sent response for request path: " + request.getPathInfo() + ", apps count: " + apps.getRegisteredApplications().size()); }
public class ConsoleDemo { public static void main(String[] args) { String[] data = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510", "\u2502Hello\u2502", "\u2514\u2500\u2500\u2500\u2500\u2500\u2518" }; for (String s : data) { System.out.println(s); } for (String s : data) { System.console().writer().println(s); } } }
/** * 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(); } }
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); }
private static void saveIndex() { assert remote2local != null; File downloadDir = new File(System.getProperty("mary.downloadDir", ".")); File licenseIndexFile = new File(downloadDir, "license-index.txt"); try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(licenseIndexFile), "UTF-8"))) { for (URL remote : remote2local.keySet()) { pw.println(remote2local.get(remote) + "|" + remote.toString()); } } catch (IOException e) { System.err.println("Problem updating the index file " + licenseIndexFile.getAbsolutePath()); e.printStackTrace(); } }
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()); } }
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) { out.println("the text"); }catch (IOException e) { System.err.println(e); }
public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("test.txt"); OutputStream os = new FileOutputStream("/tmp/test.txt"); try { BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); String line; while ((line = r.readLine()) != null) { w.println(line); } } finally { is.close(); } } }