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

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

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

PrintWriter.close介绍

[英]Closes this print writer. Flushes this writer and then closes the target. If an I/O error occurs, this writer's error flag is set to true.
[中]

代码示例

代码示例来源: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(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
}

代码示例来源: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: 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: spotbugs/spotbugs

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

代码示例来源:origin: cmusphinx/sphinx4

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

代码示例来源:origin: stanfordnlp/CoreNLP

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

代码示例来源:origin: stanfordnlp/CoreNLP

public static void main(String[] args) {
 if(args.length < minArgs) {
  System.out.println(usage());
  System.exit(-1);
   t = tf.newTreeNode(startSymbol, Collections.singletonList(t));
  pwo.println(t.toString());
  nTrees++;
 pwo.close();
 System.err.printf("Processed %d trees.%n", nTrees);

代码示例来源: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: log4j/log4j

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

代码示例来源:origin: gocd/gocd

public void typeInputToConsole(List<String> inputs) {
  for (String input : inputs) {
    processInputStream.println(input);
    processInputStream.flush();
  }
  processInputStream.close();
}

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

@Override
  public void close() {
    super.flush();
    super.close();
    setCommitted(true);
  }
}

代码示例来源: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: stanfordnlp/CoreNLP

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

代码示例来源:origin: redwarp/9-Patch-Resizer

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

代码示例来源: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: stackoverflow.com

PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
 w.print(line);
 w.flush();
 w.close();

代码示例来源:origin: stanfordnlp/CoreNLP

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

代码示例来源:origin: checkstyle/checkstyle

@Override
public void auditFinished(AuditEvent event) {
  writer.println("</checkstyle>");
  if (closeStream) {
    writer.close();
  }
  else {
    writer.flush();
  }
}

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

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

相关文章