本文整理了Java中java.io.PrintWriter
类的一些代码示例,展示了PrintWriter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PrintWriter
类的具体详情如下:
包路径:java.io.PrintWriter
类名称:PrintWriter
[英]Wraps either an existing OutputStream or an existing Writerand provides convenience methods for printing common data types in a human readable format. No IOException is thrown by this class. Instead, callers should use #checkError() to see if a problem has occurred in this writer.
[中]包装现有OutputStream或现有Writer,并提供以人类可读格式打印常见数据类型的方便方法。此类未引发IOException。相反,调用方应该使用#checkError()查看此编写器中是否出现问题。
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: stanfordnlp/CoreNLP
private Session(Socket socket) throws IOException {
client = socket;
in = new BufferedReader(new InputStreamReader(client.getInputStream(), charset));
out = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), charset));
start();
}
代码示例来源:origin: jenkinsci/jenkins
/**
* Reads the entire contents and returns it.
*/
public String read() throws IOException {
StringWriter out = new StringWriter();
PrintWriter w = new PrintWriter(out);
try (BufferedReader in = Files.newBufferedReader(Util.fileToPath(file), StandardCharsets.UTF_8)) {
String line;
while ((line = in.readLine()) != null)
w.println(line);
} catch (Exception e) {
throw new IOException("Failed to fully read " + file, e);
}
return out.toString();
}
代码示例来源:origin: stackoverflow.com
String line = "שלום, hello, привет";
OutputStream os = new FileOutputStream("c:/temp/j.csv");
os.write(239);
os.write(187);
os.write(191);
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
w.print(line);
w.flush();
w.close();
代码示例来源: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: spring-projects/spring-framework
@Override
public void write(char[] buf, int off, int len) {
super.write(buf, off, len);
super.flush();
setCommittedIfBufferSizeExceeded();
}
代码示例来源: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: spotbugs/spotbugs
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();
}
}
代码示例来源:origin: apache/incubator-pinot
public static void main(String[] args)
throws Exception {
if (args.length != 3) {
LOGGER.error("Incorrect arguments");
LOGGER.info("Usage: <exec> <UntarredSegmentDir> <QueryFile> <outputFile>");
System.exit(1);
}
String segDir = args[0];
String queryFile = args[1];
String outputFile = args[2];
String query;
ScanBasedQueryProcessor scanBasedQueryProcessor = new ScanBasedQueryProcessor(segDir);
BufferedReader bufferedReader = new BufferedReader(new FileReader(queryFile));
PrintWriter printWriter = new PrintWriter(outputFile);
while ((query = bufferedReader.readLine()) != null) {
QueryResponse queryResponse = scanBasedQueryProcessor.processQuery(query);
printResult(queryResponse);
printWriter.println("Query: " + query);
printWriter.println("Result: " + JsonUtils.objectToString(queryResponse));
}
bufferedReader.close();
printWriter.close();
}
代码示例来源:origin: stanfordnlp/CoreNLP
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in, charset));
log.info("Input some text and press RETURN to POS tag it, or just RETURN to finish.");
for (String userInput; (userInput = stdIn.readLine()) != null && ! userInput.matches("\\n?"); ) {
try {
Socket socket = new Socket(host, port);
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), charset), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream(), charset));
PrintWriter stdOut = new PrintWriter(new OutputStreamWriter(System.out, charset), true);
out.println(userInput);
stdOut.println(in.readLine());
while (in.ready()) {
stdOut.println(in.readLine());
in.close();
代码示例来源:origin: DeemOpen/zkui
public String executeCmd(String cmd, String zkServer, String zkPort) throws IOException {
StringBuilder sb;
try (Socket s = new Socket(zkServer, Integer.parseInt(zkPort)); PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()))) {
out.println(cmd);
String line = reader.readLine();
sb = new StringBuilder();
while (line != null) {
sb.append(line);
sb.append("<br/>");
line = reader.readLine();
}
}
return sb.toString();
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
String currentInfile = "";
try {
outfile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileName),"UTF-8")));
infile = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8")));
currentInfile = path.getPath();
outfile.println(SentenceUtils.listToString(sent));
} finally {
if(outfile != null)
outfile.close();
代码示例来源:origin: spotbugs/spotbugs
@Override
@ExpectWarning("PT_RELATIVE_PATH_TRAVERSAL")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String path = request.getParameter("path");
BufferedReader r = new BufferedReader(new FileReader("data/" + path));
while (true) {
String txt = r.readLine();
if (txt == null)
break;
out.println(txt);
}
out.close();
r.close();
}
代码示例来源:origin: jmxtrans/jmxtrans
private void processRequests(ServerSocket server) throws IOException {
Socket socket = server.accept();
connectionsAccepted.incrementAndGet();
try (
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), UTF_8));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8))
){
synchronized (startSynchro) {
startSynchro.notifyAll();
}
String line;
while ((line = in.readLine()) != null) {
receivedMessages.add(line);
out.print(line);
}
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
private static void resolveDummyTags(File treeFile,
TwoDimensionalCounter<String, String> pretermLabel,
TwoDimensionalCounter<String, String> unigramTagger) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(treeFile), "UTF-8"));
TreeReaderFactory trf = new FrenchTreeReaderFactory();
TreeReader tr = trf.newTreeReader(br);
PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream(new File(treeFile + ".fixed")),false,"UTF-8"));
int nTrees = 0;
for(Tree t; (t = tr.readTree()) != null;nTrees++) {
traverseAndFix(t, pretermLabel, unigramTagger);
pw.println(t.toString());
}
pw.close();
tr.close();
System.out.println("Processed " +nTrees+ " trees");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
public static PrintWriter getPrintWriter(File textFile, String encoding) throws IOException {
File f = textFile.getAbsoluteFile();
if (encoding == null) {
encoding = defaultEncoding;
}
return new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding)), true);
}
代码示例来源:origin: stanfordnlp/CoreNLP
public static void main(String[] args) throws Exception {
if (args.length != 2) {
log.info("usage: java TaggerDemo2 modelFile fileToTag");
return;
}
MaxentTagger tagger = new MaxentTagger(args[0]);
TokenizerFactory<CoreLabel> ptbTokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(),
"untokenizable=noneKeep");
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(args[1]), "utf-8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, "utf-8"));
DocumentPreprocessor documentPreprocessor = new DocumentPreprocessor(r);
documentPreprocessor.setTokenizerFactory(ptbTokenizerFactory);
for (List<HasWord> sentence : documentPreprocessor) {
List<TaggedWord> tSentence = tagger.tagSentence(sentence);
pw.println(SentenceUtils.listToString(tSentence, false));
}
// print the adjectives in one more sentence. This shows how to get at words and tags in a tagged sentence.
List<HasWord> sent = SentenceUtils.toWordList("The", "slimy", "slug", "crawled", "over", "the", "long", ",", "green", "grass", ".");
List<TaggedWord> taggedSent = tagger.tagSentence(sent);
for (TaggedWord tw : taggedSent) {
if (tw.tag().startsWith("JJ")) {
pw.println(tw.word());
}
}
pw.close();
}
代码示例来源:origin: stackoverflow.com
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString(); // stack trace as a string
代码示例来源: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: apache/ignite
File dstFile = new File(testWorkDir, "mapred-site.xml");
try (BufferedReader in = new BufferedReader(new FileReader(srcFile));
PrintWriter out = new PrintWriter(dstFile)) {
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("</configuration>"))
out.println(
" <property>\n" +
" <name>" + HadoopCommonUtils.JOB_COUNTER_WRITER_PROPERTY + "</name>\n" +
" </property>\n");
out.println(line);
out.flush();
内容来源于网络,如有侵权,请联系作者删除!