How to Access Tizen File System
PUBLISHED
Overview
Table of Content
Define features in configuration file
In order to access the filesystem, you need to add required permissions to the config.xml file. It is recommended to enable the feature using tizen SDK. Open the config.xml file, select Feature and check the http://tizen.org/api/filesystem option in tizen SDK. Else you can do it manually by adding the below line to the config.xml file.- http://tizen.org/api/filesystem: Privilege to allow access the device file system
Create a file
You can use the tizen FileSystemManagerObject to access the functionality of the filesystem API. You can access the FileSystemManagerObject using tizen.filesystem.Resolve the directory
In order to create a file or perform any operation in the file system, you need to resolve a directory and obtain an object which represents the directory, using tizen.filesystem.resolve().var documentsDir; function onResolveSuccess(dir) { documentsDir = dir; } function onResolveError(e) { console.log('message: ' + e.message); } tizen.filesystem.resolve('documents', onResolveSuccess, onResolveError, 'rw');
Create a file
You can use the object representing the directory to create a file. The createFile() takes the file name as argument.documentsDir.createFile('sample-text-file.txt');
Write to file
To gain access to the file, you need to resolve the file using the resolve() method of an object representing a directory. If the file exists, you will receive a File object representing the file handle. You need to open the file to perform operations (read/write) on it. You can use the openStream() method to open the file, which returns a FileStream object on success. The FileStream acts as a handle to the opened file. The openStream() takes up to four arguments void openStream(FileMode mode, FileStreamSuccessCallback onsuccess, optional ErrorCallback? onerror, optional DOMString? encoding);- mode – The mode in which the file will be accessed
- success callback – Called when opening was successful, receives a FileStream object
- error callback – Called when opening failed (optional)
- encoding – The encoding used in operating on the file (optional)
Mode | Usage |
---|---|
“r” | Reading |
“a” | Appending |
“rw” | Writing |
try { file = documentsDir.resolve('sample-text-file.txt'); } catch (exc) { console.log('Could not resolve file: ' + exc.message); // Stop in case of any errors return; } try { file.openStream( // open for appending 'a', // success callback - add textarea's contents writeToStream, // error callback onError ); } catch (exc) { console.log('Could not write to file: ' + exc.message); }
function writeToStream(fileStream) { try { fileStream.write('This is my first sentence to write.'); fileStream.close(); } catch (exc) { console.log('Could not write to file: ' + exc.message); }
Read from file
You have two ways to read from a file. One of the ways is to resolve the file, obtain the stream associated with the file and read from the stream.This approach is similar to the one used in write() explained above. You can get the current size of the file using the fileSize attribute of the File object.try { file = documentsDir.resolve('sample-text-file.txt'); } catch (exc) { console.log('Could not resolve file: ' + exc.message); // Stop in case of any errors return; } try { file.openStream( // open for reading 'r', // success callback - add textarea's contents readFromStream, // error callback onError ); } catch (exc) { console.log('Could not write to file: ' + exc.message); }
function readFromStream(fileStream) { try { fileStream.position = 10; var contents = fileStream.read(fileStream.bytesAvailable); fileStream.close(); console.log('file contents after pos 10:' + contents); } catch (exc) { console.log('Could not read from file: ' + exc.message); }
function displayFileContentsText() { var file; try { file = documentsDir.resolve('sample-text-file.txt'); console.log('File size: ' + file.fileSize); } catch (exc) { console.log('Could not resolve file: ' + exc.message); // Stop in case of any errors return; } try { file.readAsText( // success callback - display the contents of the file function(contents) { console.log('File contents:' + contents); }, // error callback onError ); } catch (exc) { console.log('readAsText() exception:' + exc.message + ' '); } }
Delete file
You can delete the file using deleteFile() method. It takes the file name including the full path as a parameter.function deleteSampleFile() { try { documentsDir.deleteFile( documentsDir.fullPath + '/sample-text-file.txt', } catch (exc) { console.log('deleteSampleFile(): ' + exc.message); }
Was this document helpful?
We value your feedback. Please let us know what you think.