File

API Reference: IO.File

File is an interface for accessing the filesystem of the operating system. 'path' is a required property for the construction of the File object.

Permission Management for File Operations

File operations needs permissions for Android. But for some file operations you don't need any permission as described in Google Developers. When you are reading/writing Path.DataDirectory you don't need any permission, but while you are reading/writing any of Path.android.storages path you need READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

For more information about how to manage permission in Smartface Native Framework, please visit Application Permission Management

File operations on iOS

You don't need any permission management on iOS for file operations.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import File = require("sf-core/io/file");
import Path = require('sf-core/io/path');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myFile: File
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.myFile = new File({
path: 'images://smartface.png'
});
// Checking image file if exists
if (this.myFile.exists) {
let destinationFile = new File({
path: Path.DataDirectory + '/myImage.png'
});
// If destination file exists and file sizes identical no need to copy
if (!(destinationFile.exists && this.myImage.size === destinationFile.size)) {
this.myFile.copy(destinationFile.path);
}
}
}
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const File = require('sf-core/io/file');
const Path = require('sf-core/io/path');
var Page1 = extend(Page)(
function(_super) {
_super(this);
var myImage = new File({
path: 'images://smartface.png'
});
// Checking image file if exists
if (myImage.exists) {
var destinationFile = new File({
path: Path.DataDirectory + '/myImage.png'
});
// If destination file exists and file sizes identical no need to copy
if(!(destinationFile.exists && myImage.size === destinationFile.size)){
myImage.copy(destinationFile.path);
}
}
}
);
module.exports = Page1;

Caching Data With File

File can be used like buffer in memory that holds uncompressed data blocks. A better way to retrieve relatively constant data is to use caching objects to cache the data as you retrieve it.

TypeScript
JavaScript
TypeScript
// Cache some large data
let largeDataToBeCached = {};
const filePath = Path.DataDirectory + '/file.json';
let content: string = readFile(filePath);
if (!content) {
// First time creation
createFile(filePath, JSON.stringify(largeDataToBeCached));
}
else {
// File has already been created
JSON.parse(content);
}
function createFile(path: string, content: string): void {
let file: File = new File({ path });
file.createFile(false);
let fileStream : FileStream = file.openStream(FileStream.StreamType.WRITE, FileStream.ContentMode.TEXT);
fileStream.write(content);
fileStream.close();
}
function readFile(path: string): string {
let file: File = new File({ path });
let content = null;
if (file.exists) {
let fileStream: FileStream = file.openStream(FileStream.StreamType.READ, FileStream.ContentMode.TEXT);
content = fileStream.readToEnd();
fileStream.close();
}
return content;
}
JavaScript
// Cache some large data
var largeDataToBeCached = {};
const filePath = Path.DataDirectory + '/file.json';
var content = readFile(filePath);
if (!content) {
// First time creation
createFile(filePath, JSON.stringify(largeDataToBeCached));
}
else {
// File has already been created
JSON.parse(content);
}
function createFile(path, content) {
var file = new File({ path });
file.createFile();
var fileStream = file.openStream(FileStream.StreamType.WRITE, FileStream.ContentMode.TEXT);
fileStream.write(content);
fileStream.close();
}
function readFile(path) {
var file = new File({ path });
var content = null;
if (file.exists) {
var fileStream = file.openStream(FileStream.StreamType.READ, FileStream.ContentMode.TEXT);
content = fileStream.readToEnd();
fileStream.close();
}
return content;
}