How to save the output into a file
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 afinally
block will ensure that all the resources are released regardless of any encountered exceptions.
精彩评论