Managing File Operations

This topic describes how to access and manage the files and directories in the Samsung TV file system.


Related Info


To manage application data, you can use the Filesystem API to access parts of the file system.

Accessible parts of the TV file system are represented as virtual root locations. The following are examples of virtual root locations:

  • "documents"

    Directory for text documents in formats, such as DOC and TXT. Files in this directory are not removed when the application is uninstalled.

  • "wgt-package"

    Read-only directory for application packages.

  • "wgt-private"

    Private directory for application content. Files in this directory are removed when the application is uninstalled.

  • "wgt-private-tmp"

    Temporary private directory for application content. Files in this directory are removed when the application is terminated or the Web runtime is restarted.

For information on how to manage removable storages on the device, see Handling USB Storages.

Prerequisites

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> 

Managing Files and Directories

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:

    1. Resolve the file handle:
      var sampleFile_obj;
      if(sampleDir_obj) { 
        sampleFile_obj = sampleDir_obj.resolve('sampleFile.txt');
      }
      
    2. Write to the file:
      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));
          }
        );
      }
      
    3. Read the file:
      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:

    • To delete a file, use the 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));
          }
        );
      }
      
    • To delete a directory, use the 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));
          }
        );
      }