How to Access Tizen File System

Overview

 

This article explains how to read and write to files on the tizen device. Tizen provides a simple interface, Filesystem API, to create, read, write and delete files. 

 

 

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.

 

 

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);
}

You can get the FileStream object in the success back of openStream() method. You can use the write() method on the FileStream object, which takes the text to be written to the file as input. Close the stream after the write() function call.

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);
}

After opening the file, the position of the stream is set at the beginning of the file. The position indicates which byte will be read first upon the next reading operation. Use the stream's position attribute to set the position. Reading n bytes from the file will cause the position to be moved n bytes further. The bytesAvailable attribute indicates how many bytes are still available to be read before the end of the file is reached.

You can set the position, from which the bytes are to be read, using the position attribute, and call the read() function to read all the remaining bytes from the file.

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);
}

The second way is to use the readAsText() method of the File object. Using this, you don't need to open a file stream, the success callback of readAsText() method receives the file contents as string.

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);
}