Java Reference
In-Depth Information
How to do it...
To illustrate how to save data locally, the following code snippet creates a simple application
that reads and saves data, a list of US state capitals, locally. For a full listing of the code, refer
to ch06/source-code/src/localstore/StorageDemo.fx .
var storage : Storage ;
storage = Storage {
source : "statecaps.txt"
}
var data =
"Alaska:Juneau\n"
"Arizona:Phoenix\n"
"Arkansas:Little Rock\n"
"West Virginia:Charleston\n"
"Wisconsin:Madison\n"
"Wyoming:Cheyenne\n";
var res = storage .resource;
var output= res.openOutputStream (true);
output.write (data.getBytes());
output.close();
// read it and print it
var input = res.openInputStream ();
var reader = new BufferedReader(new InputStreamReader(input));
var line:String;
while((line = reader.readLine()) != null) {
var record = line.split(":");
println ("Capital of {record[0]} is {record[1]}");
}
How it works...
The Storage API allows developers to store and retrieve data locally on the user's device. In
the previous code snippet, variable data is assigned a list (colon-separated) of United States
capitals. Then, the Storage API's resource object is used to store the data on the user's local
machine and subsequently read back from its stored location.
As mentioned, the Storage API provides two main classes as an abstraction of the local file system:
F The Storage class—this is a service class that exposes objects that facilitate the
persistence and retrieval of remove it stored resources. Its property Storage.source
provides reference to a stored resource needed to be accessed. In our example,
Storage.source points to local storage resource "statecaps.txt" . All input
or output operations will be done against that resource for the associated instance
of Storage .
 
Search WWH ::




Custom Search