Write, Bureaucrats, Administrators
4,332
edits
mNo edit summary |
mNo edit summary |
||
Line 232: | Line 232: | ||
== Working with files == | == Working with files == | ||
With scripts you can read or write files located in the device's internal memory or on the SD card. All file operations performed by the File object, which is obtained through a method call file(). | With scripts you can read or write files located in the device's internal memory or on the SD card. All file operations are performed by the File object, which is obtained through a method call file(). | ||
To work with the files | To work with the files, the library should have read/write file access. | ||
=== Global Functions === | === Global Functions === | ||
==== file(name) ==== | ==== file(name) ==== | ||
Open a file for read or write operations | Open a file for read or write operations | ||
:;Argument | :;Argument | ||
:: '''name''' — The name and the full path to the file.<br/>For example, if the file is located on the SD card, the path should | :: '''name''' — The name and the full path to the file.<br/>For example, if the file is located on the SD card, the path should be something like /sdcard/example.txt. | ||
:; Result | :; Result | ||
:: | :: File object | ||
=== Object File === | === Object File === | ||
Line 256: | Line 256: | ||
; readLine() | ; readLine() | ||
: Reads | : Reads the next line from the file | ||
:; Returns: The line | :; Returns: The line | ||
Line 264: | Line 264: | ||
; readChar() | ; readChar() | ||
: Reads | : Reads the next character | ||
:; Returns: The character | :; Returns: The character | ||
Line 286: | Line 286: | ||
<source lang="javascript" line> | <source lang="javascript" line> | ||
f = file("/sdcard/myfile.txt") | f = file("/sdcard/myfile.txt") // Open the myfile.txt file on the SD card. If the file does not exist, | ||
f.writeLine("one"); | // it will be created. | ||
f.writeLine("one"); // Write the string "one" as a line to the file | |||
f.writeLine("two"); | f.writeLine("two"); | ||
f.writeLine("three"); | f.writeLine("three"); | ||
f.close(); | f.close(); // Close the file, saving the data to the file. Until the file is closed, | ||
var a = f.readLines(); | // the disk file is still empty. | ||
var a = f.readLines(); // Read all lines of the file into the array a | |||
</source> | </source> | ||
====Save an entry into a file in XML format ==== | |||
The entry includes fields: id , title , date. | |||
====Save a file | |||
<source lang="javascript" line> | <source lang="javascript" line> | ||
var xml = '<record id="' + entry().field("id") + '">' + | var xml = '<record id="' + entry().field("id") + '">' + // Format XML record from the entry field values | ||
'<title>' + entry().field("title") + '</title>' + | '<title>' + entry().field("title") + '</title>' + | ||
'<date>' + entry().field("date") + '</date>' + | '<date>' + entry().field("date") + '</date>' + | ||
'</record>'; | '</record>'; | ||
f = file("/sdcard/" + entry().field("title") + ".xml"); | f = file("/sdcard/" + entry().field("title") + ".xml"); // Open file with same name as Entry Name | ||
f.write(xml); | f.write(xml); // Save XML data into the file | ||
f.close(); | f.close(); // Close the file | ||
</source> | </source> | ||
==Выполнение http запросов== | ==Выполнение http запросов== |