开发者

Java store xml file contents into a string

开发者 https://www.devze.com 2023-02-09 15:03 出处:网络
I have a static method that creates an xml file and I would like it to return the raw xml file as a string. Afte开发者_开发百科r creating the xml file I would like to read from the file and convert it

I have a static method that creates an xml file and I would like it to return the raw xml file as a string. Afte开发者_开发百科r creating the xml file I would like to read from the file and convert it to a string. How do I go about doing so?


http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html

Joiner.on('').join(Files.readLines(file, CharSet.fromName("UTF-8")))


you could turn to, for the file handling, to the apache.commons.io library.

This one has build in convenience functions for reading and storing files.

So for reading

org.apache.commons.io.FileUtils#readFileToString(File file) 

and for writing

org.apache.commons.io.FileUtils#writeStringToFile(File file, String data)

See here for javadoc http://commons.apache.org/io/


Here is a variety of ways of doing it:

  • How to create a Java String from the contents of a file
0

精彩评论

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

关注公众号