Reading and Writing Files

The javaxt.io.File 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 java.io classes. The javaxt.io.File 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 javaxt.io.File("/temp/file.txt").getText();

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

new javaxt.io.File("/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 javaxt.io.File("/temp/file.txt").getText("ISO-8859-1"); //read ISO-8859-1
new javaxt.io.File("/temp/file.txt").write("Hello World!", "ISO-8859-1"); //write ISO-8859-1

Serialize File Content to XML

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

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

Convert a File to an Image

You can also serialize a file directly to a JavaXT Image:

javaxt.io.Image image = new javaxt.io.File("/temp/image.jpg").getImage();

Additional Methods

Of course, the javaxt.io.File 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.