I have 5 checkboxes, 1 button, and a text file. In my text file the values are stored like this:
1,0,0,0,1
When I run the program, the first and last checkboxes need to be checked and the others need to be unchecked.
If I check or uncheck any of the checkboxes then click the save button, the changed values have to be updated in text file. How can i achieve this?
var filename:String = "checkboxes.txt";
var checkboxes:Vector.<CheckBox> = new <CheckBox>[checkbox1, checkbox2, etc];
function load():void {
// read text file
var file:File = File.applicationStorageDirectory.resolvePath(filename);
if(!file.exists)
return;
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var text:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
// decode text values to checkboxes
var values:Array = text.split(",");
for(var i:int = 0; i < values.length; i++){
var checked:Boolean = values[i] == "1";
checkboxes[i].selected = checked;
}
}
function save():void {
// encode checkboxes to text
var values:Array = [];
for(var i:int = 0; i < checkboxes.length; i++){
values.push(int(checkboxes[i].selected));
}
var text:String = values.join(",");
// write to file
var file:File = File.applicationStorageDirectory.resolvePath(filename);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTFBytes(text);
fileStream.close();
}