开发者

How to save the output into a file

开发者 https://www.devze.com 2023-03-05 16:28 出处:网络
In a program, I currently have process.serialize(System.out); This generally output the object \"开发者_Python百科process\" to the screen in a XML format.

In a program, I currently have

process.serialize(System.out);

This generally output the object "开发者_Python百科process" to the screen in a XML format.

But I want to redirect this XML format into a saved file, like test.XML. How to do that in java? Thanks.


System.out is a of PrintStream type so you should have a look here. Using PrintStream combined with a File like this:

process.serialize(new PrintStream(new File("my file")));

will do the work.


this specifically goes like

process.serialize(new PrintStream("test.xml"));


I agree with the previous answers by equality and ratchet freak, and I'd like to add a few points:

  • You can learn more about different ways to work with streams and files from Sun/Oracle's tutorial on I/O.
  • It's important to close every stream you use, by calling the stream's close() method. Placing those calls in a finally block will ensure that all the resources are released regardless of any encountered exceptions.
0

精彩评论

暂无评论...
验证码 换一张
取 消