English 中文(简体)
如何用BufferedWriter向标准投稿
原标题:How to write to Standard Output using BufferedWriter
  • 时间:2010-12-10 02:15:22
  •  标签:
  • java

目前,我正在撰写一份使用BufferedWriter制作数份记录文档的申请。 然而,在进行欺骗时,我想写的是系统,而不是档案。 图一

log = new BufferedWriter(new FileWriter(tokenizerLog));

:

BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
log.write("Log output
");

as opposed :

System.out.println("log output")

新的<条码>OutputStreamWriter 备选办法尚未奏效。 我如何改变布图德韦里建筑商内部的目的,从档案转向标准。 由于我有数份日志,我将使用系统书写。 在任何地方,把产出改变为档案,这实际上是一种选择。

最佳回答

你的做法确实奏效,你只是想弄清产出:

try {    
  BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));

  log.write("This will be printed on stdout!
");
  log.flush();
}
catch (Exception e) {
  e.printStackTrace();
}

The both OutputStreamWriter and PrintWriter are Writer cases sobre You can Just do such as:

BufferedWriter log;

Writer openForFile(String fileName) {
  if (fileName != null)
    return new PrintWriter(fileName);
  else
    return new OutputStreamWriter(System.out);
}

log = new BufferedWriter(openForFile(null)); //stdout
log = new BufferedWriter(openForFile("mylog.log")); // using a file

不管怎样,这只是给你这个想法。

问题回答

由于你提到这是伐木,你可能想看像标志4j那样使用一个伐木图书馆。 这使你只改变组合档案,从而改变标识的目的地(无论是原木档案还是ole)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签