This topic describes how to access and manage the files and directories in the SSSP file system.
To manage application data, you can use the Filesystem API to access parts of the file system.
Accessible parts of the SSSP file system are represented as virtual root locations. The following are examples of virtual root locations:
For information on how to manage removable storages on the device, see Handling USB Storages.
Note
If you want to store less than 5 MB of data, use the Web storage.
To use the Filesystem API, the application has to request permission by adding the following privileges to the "config.xml" file:
<tizen:privilege name='http://tizen.org/privilege/filesystem.read'></tizen:privilege>
<tizen:privilege name='http://tizen.org/privilege/filesystem.write'></tizen:privilege>
To manage files and directories:
To access a specific file or directory, you must first retrieve a file handle using the resolve()
method:
var documents_obj;
tizen.filesystem.resolve(
'documents',
function(obj) {
documents_obj = obj;
},
function(error) {
console.log(JSON.stringify(error));
},
'rw'
);
The file handle is returned in the success event handler, and is a reference object that points to and represents a file or directory.
To create files and directories:
To create a directory within the file system, use the createDirectory()
method.
The directory is created relative to the current directory where the operation is performed.
if (documents_obj) {
documents_obj.createDirectory('sampleDir');
}
To create a file, use the createFile()
method:
var sampleDir_obj;
tizen.filesystem.resolve(
'documents/sampleDir',
function(obj) {
sampleDir_obj = obj;
},
function(error) {
console.log(JSON.stringify(error));
},
'rw'
);
if (sampleDir_obj) {
sampleDir_obj.createFile('sampleFile.txt');
}
To write to a file and read it, use the openStream()
method:
var sampleFile_obj;
if(sampleDir_obj) {
sampleFile_obj = sampleDir_obj.resolve('sampleFile.txt');
}
if (sampleFile_obj) {
sampleFile_obj.openStream(
'a',
function(fileStream) {
fileStream.write('This is just sample text.');
fileStream.close();
},
function(error) {
console.log(JSON.stringify(error));
}
);
}
if (sampleFile_obj) {
sampleFile_obj.openStream(
'r',
function(fileStream) {
fileStream.position = 0;
contents = fileStream.read(fileStream.bytesAvailable);
console.log(contents);
fileStream.close();
},
function(error) {
console.log(JSON.stringify(error));
}
);
}
To delete files and directories:
deleteFile()
method:if (sampleDir_obj) {
sampleDir_obj.deleteFile(
sampleDir_obj.fullPath + '/sampleFile.txt',
function() {
console.log('deleteFile Success');
},
function(error) {
console.log(JSON.stringify(error));
}
);
}
deleteDirectory()
method:if (documents_obj) {
documents_obj.deleteDirectory(
documents_obj.fullPath + '/sampleDir',
false,
function() {
console.log('deleteDirectory Success');
},
function(error) {
console.log(JSON.stringify(error));
}
);
}