aboutsummaryrefslogtreecommitdiff
path: root/java/FileExample.java
blob: 6f180d9dfbdbe54b3fcfbdcd98e184cb5012c425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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") ) );

			root.get("aoeu");
			root.getDict("aoeu");
			root.getList("aoeu");
			root.getInt("aoeu");
			root.getFloat("aoeu");
			root.getBool("aoeu");
			root.getString("aoeu");
		}
		catch( FileNotFoundException e )
		{
			System.out.println("Error opening file.");
		}
		catch( IOException e )
		{
			System.out.println("Error reading data.");
		}
		catch( KeyNotFoundException e )
		{
			System.out.println("Key not found.");
		}
	}

	public static void main( String args[] )
	{
		System.out.println("GATS file example.");
		writeFile();
		readFile();
	}
}