AIRでXMLを保存したいなーとかおもっていたので、作ってみました。
非同期にしようか迷ったのですが、今回は実行したら値を返してもらいたかったので、非同期にしませんでした。
というか、非同期にすると表示に関する処理とロジックの切り分けとかがうまくできないんです・・・。
だれか教えてくれるとたすかります・・・。
ちなみに、「applicationStorageDirectory」の保存先をしらべたところ、以下のような感じでした。
- Windows Vista
- C:¥Users¥username¥AppData¥Roaming¥appId¥Local Store
- Mac
- /Users/username/Library/Preferences/appId/Local Store
XmlStorage
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
|
/**
* XML読み込み、保存用クラス
* version 0.1
*/
package app.util
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import mx.controls.Alert;
public class XmlStorage
{
public var filename:String;
public function XmlStorage()
{
}
/**
* データの保存
*
*/
public function save(xmlData:XML,filename:String=""):void
{
if (filename == "") {
filename = this.filename;
}
try {
var file:File = File.applicationStorageDirectory;
file = file.resolvePath(filename);
var outputData:String = '<?xml version="1.0" encoding="utf-8"?>\n';
outputData += xmlData.toXMLString();
outputData = outputData.replace(/\n/g, File.lineEnding);
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(outputData);
stream.close();
} catch(e:Error) {
Alert.show(e.message,"SaveError");
}
}
/**
* ファイルの読み込み
*/
public function read(filename:String=""):XML
{
if (filename == "") {
filename = this.filename;
}
try {
var file:File = File.applicationStorageDirectory;
file = file.resolvePath(filename);
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var xmlData:XML = XML(stream);
stream.close();
return xmlData;
}catch(e:Error) {
Alert.show(e.message,"read error");
if (stream != null) {
stream.close();
}
}
return null;
}
}
}
|