-2

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?

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
Peiman3D
  • 15
  • 3

1 Answers1

0
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();
}
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
  • i create text file in current path but have error in run errors: Line 6 1046: Type was not found or was not a compile-time constant: File. Line 7 1046: Type was not found or was not a compile-time constant: FileStream. Line 6 1180: Call to a possibly undefined method File. Line 7 1180: Call to a possibly undefined method FileStream. Line 8 1120: Access of undefined property FileMode. Line 29 1046: Type was not found or was not a compile-time constant: File. Line 30 1046: Type was not found or was not a compile-time constant: FileStream. – Peiman3D Apr 07 '15 at 16:39
  • In this case you should use `app-storage:` url scheme, or `File.applicationStorageDirectory`. I'll update my example. – Aaron Beall Apr 07 '15 at 16:45
  • You also need to import all the classes in use, for example `import flash.filesystem.File` – Aaron Beall Apr 07 '15 at 16:56
  • very tanx but have error in import flash.filesystem.File error: 1046: Type was not found or was not a compile-time constant: File. 1046: Type was not found or was not a compile-time constant: FileStream. – Peiman3D Apr 08 '15 at 20:25
  • Are you using AIR like you tagged for this question? The `File` class is only available in AIR, not the Flash Player. – Aaron Beall Apr 08 '15 at 20:43
  • many tanx ... I can use this code In "Air For Android" in flash cs6? – Peiman3D Apr 09 '15 at 07:25
  • Yes, you can use the `File` class in AIR for Android. – Aaron Beall Apr 09 '15 at 13:29
  • tanx for help run Was this code white error in output: [SWF] Untitled-2_Scene%201.swf - 33887 bytes after decompression Error: Error #3003: File or directory does not exist. at flash.filesystem::FileStream/open() at Untitled_fla::MainTimeline/frame1()[Untitled_fla.MainTimeline::frame1:17] at runtime::ContentPlayer/loadInitialContent() at runtime::ContentPlayer/playRawContent() at runtime::ContentPlayer/playContent() at runtime::AppRunner/run() at ADLAppEntry/run() at global/runtime::ADLEntry() [UnloadSWF] Untitled-2_Scene%201.swf Test Movie terminated. – Peiman3D Apr 10 '15 at 12:07
  • You cannot `open()` a file as `FileMode.READ` if the file doesn't exist. You can check if a file exists by using `File/exists`. I'll update my example to take this into consideration. – Aaron Beall Apr 10 '15 at 14:49
  • many tanx ... this is code run very good and no error ... but in run checkbox no checked... i save Each file in Desktop and create text file in Desktop(current)...where is my problem? – Peiman3D Apr 10 '15 at 19:52
  • Ooooh my GOD...i change "File.applicationStorageDirectory.resolvePath(filename);" to "File.applicationDirectory.resolvePath(filename);" and it is run without problem and work Properly. – Peiman3D Apr 10 '15 at 20:10
  • Cool, yes you can put the file wherever you want. Just make sure both `save()` and `load()` use the same file location. ;) – Aaron Beall Apr 10 '15 at 20:47
  • now just problem is in save : SecurityError: fileWriteResource at flash.filesystem::FileStream/open() – Peiman3D Apr 10 '15 at 21:04
  • now just is 1 Problem... in read file " var file:File=File.applicationDirectory.resolvePath(filename);" is work Properly but in write file not work... in write file "var file:File = File.desktopDirectory.resolvePath(filename);" work Properly ... and my problem is desktop directory... i need read & write file only current file. – Peiman3D Apr 10 '15 at 21:34
  • Well, `applicationDirectory` often does not allow write, this is why you usually use `applicationStorageDirectory`. – Aaron Beall Apr 13 '15 at 20:41