From e3efaf2a9ad82deb1644ccab8c1469719a0c5b65 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 16 Jan 2012 02:31:34 +0000 Subject: Lots of documentation, an example program, and also some visibility cleanup. --- java/FileExample.java | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 java/FileExample.java (limited to 'java/FileExample.java') diff --git a/java/FileExample.java b/java/FileExample.java new file mode 100644 index 0000000..f85f88d --- /dev/null +++ b/java/FileExample.java @@ -0,0 +1,94 @@ +import com.xagasoft.gats.*; + +import java.io.FileInputStream; +import java.io.FileOutputStream; + +import java.io.FileNotFoundException; +import java.io.IOException; + +class FileExample +{ + public static void writeFile() + { + System.out.println(); + System.out.println("Creating a new gats object tree..."); + + GatsDictionary root = new GatsDictionary(); + + // Automatically determine the type of gats object and insert it + // for you + root.put("String", "This is a string"); + root.put("Integer", 445 ); + root.put("Float", 44.5 ); + root.put("Boolean", true ); + + // Ensure the correct type and insert it yourself + root.put("ExplicitFloat", new GatsFloat( 44 ) ); + + GatsList lst = new GatsList(); + lst.add( new GatsString("Hello") ); + lst.add( new GatsInteger( 314159 ) ); + lst.add( new GatsFloat( 3.14159 ) ); + + root.put("List", lst ); + + GatsDictionary subDict = new GatsDictionary(); + subDict.put("name", "Bob"); + root.put("Dictionary", subDict ); + + try + { + System.out.println("Writing tree to file: test.gats"); + FileOutputStream fos = new FileOutputStream("test.gats"); + GatsOutputStream gos = new GatsOutputStream( fos ); + int iBytes = gos.writeObject( root ); + System.out.println("Wrote " + iBytes + " total bytes."); + } + catch( FileNotFoundException e ) + { + System.out.println("Error opening file."); + } + catch( IOException e ) + { + System.out.println("Error writing data."); + } + } + + public static void readFile() + { + System.out.println(); + try + { + System.out.println("Reading in gats file: test.gats"); + FileInputStream fis = new FileInputStream("test.gats"); + GatsInputStream gis = new GatsInputStream( fis ); + GatsObject obj = gis.readObject(); + System.out.println("Full object: " + obj ); + + GatsDictionary root = (GatsDictionary)obj; + System.out.println("An integer: " + root.getInt("Integer") ); + + // This is a little akward since getString returns a byte array, + // so if you want to use the data as a java string you have to build + // the string object yourself. + System.out.println("Sub string: " + new String( + root.getDict("Dictionary").getString("name") ) ); + } + catch( FileNotFoundException e ) + { + System.out.println("Error opening file."); + } + catch( IOException e ) + { + System.out.println("Error reading data."); + } + } + + public static void main( String args[] ) + { + System.out.println("GATS file example."); + writeFile(); + readFile(); + } +} + -- cgit v1.2.3