2017년 6월 22일 목요일

how treat exceptions in app inventor ?


I am creating a file  .txt using savefile, but i need to know if de file exists or no before create it.

--
If you call File1.ReadFrom fileName = MyFile and MyFile does not exist, you get a notification  " Error 2101:  The file MyFile could not be found"

Also, if  perhaps    set label1.text =  File1.ReadFrom fileName = MyFile and then check length label1.text is not zero, the file does not exist as a text file?    Did not check that.

However, you should be able to use AppendToFile to create file if it does not exist.  See comments below.

File
Non-visible component for storing and retrieving files. Use this component to write or read files on your device. The default behaviour is to write files to the private data directory associated with your App. The Companion is special cased to write files to /sdcard/AppInventor/data to facilitate debugging. If the file path starts with a slash (/), then the file is created relative to /sdcard. For example writing a file to /myFile.txt will write the file in /sdcard/myFile.txt.

Properties

none

Events

GotText(text text)
Event indicating that the contents from the file have been read.

Methods

AppendToFile(text text, text fileName)
Appends text to the end of a file storage, creating the file if it does not exist. See the help text under SaveFile for information about where files are written.

Delete(text fileName)
Deletes a file from storage. Prefix the filename with / to delete a specific file in the SD card, for instance /myFile.txt. will delete the file /sdcard/myFile.txt. If the file does not begin with a /, then the file located in the programs private storage will be deleted. Starting the file with // is an error because assets files cannot be deleted.

ReadFrom(text fileName)
Reads text from a file in storage. Prefix the filename with / to read from a specific file on the SD card. for instance /myFile.txt will read the file /sdcard/myFile.txt. To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a filename does not start with a slash, it will be read from the applications private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.

SaveFile(text text, text fileName)
Saves text to a file. If the filename begins with a slash (/) the file is written to the sdcard. For example writing to /myFile.txt will write the file to /sdcard/myFile.txt. If the filename does not start with a slash, it will be written in the programs private data directory where it will not be accessible to other programs on the phone. There is a special exception for the AI Companion where these files are written to /sdcard/AppInventor/data to facilitate debugging. Note that this block will overwrite a file if it already exists. If you want to add content to a file use the append block.

--
Use the Screen1.WhenErrorOccurred block to intercept exceptions.

--

댓글 없음:

댓글 쓰기