diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-01-16 02:31:34 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-01-16 02:31:34 +0000 |
commit | e3efaf2a9ad82deb1644ccab8c1469719a0c5b65 (patch) | |
tree | dc1b3f3c787dd5900e80aaeca1a46cd9696d230b /java/FileExample.java | |
parent | 66f972a288916824d9001c1931bf4c5db1bed82b (diff) | |
download | libgats-e3efaf2a9ad82deb1644ccab8c1469719a0c5b65.tar.gz libgats-e3efaf2a9ad82deb1644ccab8c1469719a0c5b65.tar.bz2 libgats-e3efaf2a9ad82deb1644ccab8c1469719a0c5b65.tar.xz libgats-e3efaf2a9ad82deb1644ccab8c1469719a0c5b65.zip |
Lots of documentation, an example program, and also some visibility cleanup.
Diffstat (limited to 'java/FileExample.java')
-rw-r--r-- | java/FileExample.java | 94 |
1 files changed, 94 insertions, 0 deletions
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 @@ | |||
1 | import com.xagasoft.gats.*; | ||
2 | |||
3 | import java.io.FileInputStream; | ||
4 | import java.io.FileOutputStream; | ||
5 | |||
6 | import java.io.FileNotFoundException; | ||
7 | import java.io.IOException; | ||
8 | |||
9 | class FileExample | ||
10 | { | ||
11 | public static void writeFile() | ||
12 | { | ||
13 | System.out.println(); | ||
14 | System.out.println("Creating a new gats object tree..."); | ||
15 | |||
16 | GatsDictionary root = new GatsDictionary(); | ||
17 | |||
18 | // Automatically determine the type of gats object and insert it | ||
19 | // for you | ||
20 | root.put("String", "This is a string"); | ||
21 | root.put("Integer", 445 ); | ||
22 | root.put("Float", 44.5 ); | ||
23 | root.put("Boolean", true ); | ||
24 | |||
25 | // Ensure the correct type and insert it yourself | ||
26 | root.put("ExplicitFloat", new GatsFloat( 44 ) ); | ||
27 | |||
28 | GatsList lst = new GatsList(); | ||
29 | lst.add( new GatsString("Hello") ); | ||
30 | lst.add( new GatsInteger( 314159 ) ); | ||
31 | lst.add( new GatsFloat( 3.14159 ) ); | ||
32 | |||
33 | root.put("List", lst ); | ||
34 | |||
35 | GatsDictionary subDict = new GatsDictionary(); | ||
36 | subDict.put("name", "Bob"); | ||
37 | root.put("Dictionary", subDict ); | ||
38 | |||
39 | try | ||
40 | { | ||
41 | System.out.println("Writing tree to file: test.gats"); | ||
42 | FileOutputStream fos = new FileOutputStream("test.gats"); | ||
43 | GatsOutputStream gos = new GatsOutputStream( fos ); | ||
44 | int iBytes = gos.writeObject( root ); | ||
45 | System.out.println("Wrote " + iBytes + " total bytes."); | ||
46 | } | ||
47 | catch( FileNotFoundException e ) | ||
48 | { | ||
49 | System.out.println("Error opening file."); | ||
50 | } | ||
51 | catch( IOException e ) | ||
52 | { | ||
53 | System.out.println("Error writing data."); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | public static void readFile() | ||
58 | { | ||
59 | System.out.println(); | ||
60 | try | ||
61 | { | ||
62 | System.out.println("Reading in gats file: test.gats"); | ||
63 | FileInputStream fis = new FileInputStream("test.gats"); | ||
64 | GatsInputStream gis = new GatsInputStream( fis ); | ||
65 | GatsObject obj = gis.readObject(); | ||
66 | System.out.println("Full object: " + obj ); | ||
67 | |||
68 | GatsDictionary root = (GatsDictionary)obj; | ||
69 | System.out.println("An integer: " + root.getInt("Integer") ); | ||
70 | |||
71 | // This is a little akward since getString returns a byte array, | ||
72 | // so if you want to use the data as a java string you have to build | ||
73 | // the string object yourself. | ||
74 | System.out.println("Sub string: " + new String( | ||
75 | root.getDict("Dictionary").getString("name") ) ); | ||
76 | } | ||
77 | catch( FileNotFoundException e ) | ||
78 | { | ||
79 | System.out.println("Error opening file."); | ||
80 | } | ||
81 | catch( IOException e ) | ||
82 | { | ||
83 | System.out.println("Error reading data."); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | public static void main( String args[] ) | ||
88 | { | ||
89 | System.out.println("GATS file example."); | ||
90 | writeFile(); | ||
91 | readFile(); | ||
92 | } | ||
93 | } | ||
94 | |||