Reading and Writing Files

The class makes it easy to read and write files. Here are a few simple examples:

Read/Write Text From a File

Something as simple as reading text from a file requires multiple lines of code using the core classes. The class lets you read/write text in a single line of code! Here's an example of how to serialize content from a file to a String.

String text = new"/temp/file.txt").getText();

Here's an example of how to write text to a file.

new"/temp/file.txt").write("Hello World!");

Note that the getText() and write() used in these examples use UTF-8. You can pass in a different character set like this:

String text = new"/temp/file.txt").getText("ISO-8859-1"); //read ISO-8859-1
new"/temp/file.txt").write("Hello World!", "ISO-8859-1"); //write ISO-8859-1

Serialize File Content to XML

With the class, you can easily serialize a file to an XML DOM Document like this:

org.w3c.dom.Document xml = new"/temp/file.xml").getXML();

Convert a File to an Image

You can also serialize a file directly to a JavaXT Image: image = new"/temp/image.jpg").getImage();

Additional Methods

Of course, the class can be used to return a raw byte array, input stream, buffered reader, etc. Please refer to the JavaDocs for a complete list of methods.