Class FileCtrl


  • public class FileCtrl
    extends java.lang.Object
    This controller is used to read / write file to storage with system permission.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      android.os.Bundle canRead​(java.lang.String path)
      Tests whether the application can read the file denoted by this abstract pathname.
      android.os.Bundle canWrite​(java.lang.String path)
      Tests whether the application can modify the file denoted by this abstract pathname.
      android.os.Bundle closeFileStream​(long fileStreamId)
      Close the specific file stream that created by FileCtrl.createFileStream.
      android.os.Bundle copyFile​(java.lang.String fromPath, java.lang.String toPath)
      Copy file bundle.
      android.os.Bundle createFile​(java.lang.String path)
      Create file or folder on device
      android.os.Bundle createFileStream​(java.lang.String filepath, boolean keepContents)
      Create a stream that you can use the stream to read or write data into the file.
      If you have completed your operation on this stream, you should call FileCtrl.closeFileStream(fileStreamId) to close this stream.
      android.os.Bundle deleteFile​(java.lang.String path)
      Deletes the file or folder.
      android.os.Bundle exists​(java.lang.String path)
      Check if the file or folder exists.
      static FileCtrl getInstance​(android.content.Context context)
      Gets instance.
      android.os.Bundle isDirectory​(java.lang.String path)
      Check if path is directory.
      android.os.Bundle list​(java.lang.String path)
      Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
      android.os.Bundle openFile​(java.lang.String path)
      open file for the given package name and path
      android.os.Bundle readFromFile​(java.lang.String path)
      Read from file.
      android.os.Bundle renameTo​(java.lang.String path, java.lang.String newName)
      Renames the file denoted by this name.
      android.os.Bundle writeToFile​(java.lang.String path, byte[] content)
      Write to file with byte[]
      android.os.Bundle writeUTF8ToFile​(java.lang.String path, java.lang.String content)
      Write String(utf8) to file.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getInstance

        public static FileCtrl getInstance​(android.content.Context context)
        Gets instance.
        Parameters:
        context - the context
        Returns:
        the instance
      • writeUTF8ToFile

        public android.os.Bundle writeUTF8ToFile​(java.lang.String path,
                                                 java.lang.String content)
        Write String(utf8) to file.
        Parameters:
        path - the path (String)
        content - the content (String)
        Returns:
        A bundle contains the error code and message
        int erroeCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • writeToFile

        public android.os.Bundle writeToFile​(java.lang.String path,
                                             byte[] content)
        Write to file with byte[]
        Parameters:
        path - the path (String)
        content - the content (String)
        Returns:
        A bundle contains the error code and message
        int erroeCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • readFromFile

        public android.os.Bundle readFromFile​(java.lang.String path)
        Read from file.
        Parameters:
        path - the path (String)
        Returns:
        A bundle contains the error code and message
        int erroeCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • copyFile

        public android.os.Bundle copyFile​(java.lang.String fromPath,
                                          java.lang.String toPath)
        Copy file bundle.
        Parameters:
        fromPath - the from path (String)
        toPath - the to path (String)
        Returns:
        A bundle contains the error code and message
        int erroeCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • createFile

        public android.os.Bundle createFile​(java.lang.String path)
        Create file or folder on device
        Parameters:
        path - The path of file or folder. If you want to create folder, the path should end with "/" like "/sdcard/newfolder/" (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • deleteFile

        public android.os.Bundle deleteFile​(java.lang.String path)
        Deletes the file or folder. If this pathname denotes a folder, then the folder must be empty in order to be deleted.
        Parameters:
        path - The path of file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • createFileStream

        public android.os.Bundle createFileStream​(java.lang.String filepath,
                                                  boolean keepContents)
        Create a stream that you can use the stream to read or write data into the file.
        If you have completed your operation on this stream, you should call FileCtrl.closeFileStream(fileStreamId) to close this stream.
        Parameters:
        filepath - The file path that you want to make a stream on it. (String)
        keepContents - Whether you want to keep the file contents. If you want to read data from the stream, you should set keepContents as true. (true: keep, false: erase file) (boolean)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
        ParcelFileDescriptor pfd = bundle.getParcelable(FileCtrl.BUNDLE_PARCEL_FILE_DESCRIPTOR);
        long fileStreamId = bundle.getLong(FileCtrl.BUNDLE_FILE_STREAM_ID);
      • closeFileStream

        public android.os.Bundle closeFileStream​(long fileStreamId)
        Close the specific file stream that created by FileCtrl.createFileStream.
        Parameters:
        fileStreamId - The file stream ID that you want to close. (long)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
        String errorMsg = bundle.getString("errorMsg");
      • exists

        public android.os.Bundle exists​(java.lang.String path)
        Check if the file or folder exists.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: file or folder does not exist, 0: file or folder exists)
        String errorMsg = bundle.getString("errorMsg");
      • isDirectory

        public android.os.Bundle isDirectory​(java.lang.String path)
        Check if path is directory.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: path is not directory, 0: path is directory)
        String errorMsg = bundle.getString("errorMsg");
      • openFile

        public android.os.Bundle openFile​(java.lang.String path)
        open file for the given package name and path
        Parameters:
        path - The path of the file. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: Cannot open the file, 0: success to open the file)
        String errorMsg = bundle.getString("errorMsg");
      • list

        public android.os.Bundle list​(java.lang.String path)
        Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: list is null, 0: list is not null)
        String errorMsg = bundle.getString("errorMsg");
      • renameTo

        public android.os.Bundle renameTo​(java.lang.String path,
                                          java.lang.String newName)
        Renames the file denoted by this name.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
        String errorMsg = bundle.getString("errorMsg");
      • canWrite

        public android.os.Bundle canWrite​(java.lang.String path)
        Tests whether the application can modify the file denoted by this abstract pathname.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
        String errorMsg = bundle.getString("errorMsg");
      • canRead

        public android.os.Bundle canRead​(java.lang.String path)
        Tests whether the application can read the file denoted by this abstract pathname.
        Parameters:
        path - The path of the file or folder. (String)
        Returns:
        A bundle contains the error code and message
        int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
        String errorMsg = bundle.getString("errorMsg");