java.io.PrintWriter.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(98)

本文整理了Java中java.io.PrintWriter.<init>()方法的一些代码示例,展示了PrintWriter.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PrintWriter.<init>()方法的具体详情如下:
包路径:java.io.PrintWriter
类名称:PrintWriter
方法名:<init>

PrintWriter.<init>介绍

[英]Constructs a new PrintWriter with file as its target. The VM's default character set is used for character encoding. The print writer does not automatically flush its contents to the target file when a newline is encountered. The output to the file is buffered.
[中]以文件为目标构造新的PrintWriter。VM的默认字符集用于字符编码。遇到换行符时,打印写入程序不会自动将其内容刷新到目标文件。文件的输出被缓冲。

代码示例

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);
  }
}

代码示例来源:origin: stackoverflow.com

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString(); // stack trace as a string

代码示例来源:origin: stackoverflow.com

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
}

代码示例来源:origin: stackoverflow.com

try(  PrintWriter out = new PrintWriter( "filename.txt" )  ){
  out.println( text );
}

代码示例来源:origin: stackoverflow.com

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();
  }
}

代码示例来源:origin: apache/incubator-dubbo

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();
  }
}

代码示例来源:origin: brianway/webporter

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);
  }
}

代码示例来源:origin: stackoverflow.com

try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) {
  out.println("the text");
}catch (IOException e) {
  System.err.println(e);
}

代码示例来源:origin: apache/flink

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();
  }
}

代码示例来源:origin: apache/incubator-pinot

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());
 }
}

代码示例来源:origin: stackoverflow.com

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.

代码示例来源:origin: apache/incubator-gobblin

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();
 }
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * 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();
  }
}

代码示例来源:origin: apache/flink

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();
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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));
}

代码示例来源:origin: ctripcorp/apollo

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;
 }
}

代码示例来源:origin: twitter/distributedlog

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);
}

代码示例来源:origin: apache/incubator-dubbo

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();
  }
}

代码示例来源:origin: alibaba/druid

public static String getStackTrace(Throwable ex) {
  StringWriter buf = new StringWriter();
  ex.printStackTrace(new PrintWriter(buf));
  return buf.toString();
}

相关文章