FileStream dosen´t work in actionscript 3 (Adobe Air) - actionscript-3

I have a little problem here.. The code itself dosen´t get any errors, but it will not write to the .txt file... Hope someone can help me with this problem.
Here is my code:
test_btn.addEventListener(MouseEvent.CLICK, leggtil);
function leggtil(e:MouseEvent) {
var file:File = File.documentsDirectory.resolvePath("test.txt");
var fileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var outputString:String = "test";
fileStream.writeUTFBytes(outputString);
fileStream.close();
}

I tested the code and it worked on my machine.
I believe that the code is working for you, but you're not aware of where the file has been written. Check your documents folder. In Windows, this would be C:\Users\[your user name]\Documents\. If you browse to that folder, you should see a file there named test.txt.

Related

Set the file extension of the temp file

In AIR there is the File.createTempFile() method docs.
Is there a way to change the extension?
Code so far:
var webpage:File = File.createTempFile();
webpage.extension = "html"; // Property is read-only
The simple answer is: no.
You're even not able to set the filename by yourself. As soon as you call File.createTempFile(); it will create a file like fla8121.tmp which is composed of the prefix 'fla' a random 4-digit hex number and finally the '.tmp' file extension.
On windows this file will be created in C:\Users\[Username]\AppData\Local\Temp\
If you want to create a temporary file on your own, you need to do it like this:
var file:File = File.cacheDirectory.resolvePath("test.html");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes("<html>This is a test</html>");
stream.close();
This will create test.html in the same directory as createTempFile()

Loading files in Android Mobile using AIR and Actionscript 3.0

Im building a Test Maker App. I need some help on loading files in Android mobile. The type i want is the type where the user can look for his file from a file browser. This is the code i have so far but it wont work. My mobile always says "No files were found".
var file = File.applicationStorageDirectory.resolvePath("saved projects");//This is a folder
file.browseForOpen("Open Your File", [new FileFilter("Text Files", "*.txt")]);
file.addEventListener(Event.SELECT, onFileSelected);
public function onFileSelected(e:Event):void {
var stream:FileStream = new FileStream();
stream.open(e.target as File, FileMode.READ);
loadTxt.text = stream.readUTF();
}
Plus additional question: Where is the best location to save files in the ANdroid mobile?
You can write your file manager, it should not be difficult. Scan a directory with method "getDirectoryListing":
var file:File = File.applicationDirectory.resolvePath('myDirectory/');
var filesList:Array = file.getDirectoryListing();
and show a list of files.

Adobe Air - AS3 - Listing files in directory without the extension

I'm listing files in the "applicationDirectory" and getting the names with the extension,
For example "myVid.mp4".
I would like to get only the filename without the extension(mp4)
Here is my code so far
var subjDir:File = File.applicationDirectory.resolvePath("videos");
var files:Array = subjDir.getDirectoryListing();
var filesName:Array = new Array();
for (var i:uint=0; i<files.length;i++)
{
trace(files[i].name);
trace(files[i].nativePath);
filesName.push({label:files[i].name,fpath:files[i].nativePath});
}
Thank's in advance!
trace (files[i].name.substr(0, files[i].name.lastIndexOf('.')));
This will remove the file extension:
trace(files[i].name.replace(/\.[^\.]*$/,""));

How to create NEW txt file using Adobe AIR

How to create a REALY NEW text file (.txt) using AS3 adobe AIR. Most articles are writing to EXISTING text file (text file already exist).
like this one:
How to create new File txt by using Adobe Air
Thank you.
//create a reference to the file in the applicationStorage Directory
//(for more directories, look at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var fileToCreate:File = File.applicationStorageDirectory;
fileToCreate = fileToCreate.resolvePath("myfile.txt");
//create a filestream to write to the file
//the write and/or creating the file is done with the FileMode in the open() function
//look at the table that describes what FileMode does what here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileMode.html
var fileStream:FileStream = new FileStream();
fileStream.open(fileToCreate, FileMode.WRITE);
//write some string to the file
fileStream.writeUTF('this is a string to write to the file');
//finally, close the filestream
fileStream.close();
EDIT: changed filemode from READ to WRITE. READ does not create the file ofcourse :)

AIR Native process Error

I try to launch a programm with NativeProcess on Mac.
pathEV="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath(pathEV);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
But this error appear:
Error #3214: NativeProcessStartupInfo.executable does not specify a valid executable file
Can you help me to solve that?
Thanks
Haven't tested it yet, but I assume you'd need to run open with the path to your application as a parameter, like this:
pathEV:String="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath( '/usr/bin/open' );
nativeProcessStartupInfo.arguments = Vector.<String>([pathEV]);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
And yes, this definitely needs extendedDesktop rights
FYI: The Vector shizzle is correct, I use it to convert the array to Vector, not as a constructor function
Here's an explanation how to launch .app files from the terminal
Could it be that you need to add <supportedProfiles>extendedDesktop</supportedProfiles> to your manifest file?
Also, I believe.app is not an executable. Its actually just an application bundle within which exists the actual executable. Try pointing to /bin/ls first to make sure all else is OK and then dig inside the .app folder to find your executable.
Why do you need a nativeProcess? Why not just use fileEV.openWithDefaultApplication() ?