Skip to main content
Version: 7.3.0

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 on Android 12L (API Level 32) and below, granular media permissions (READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO) on Android 13 (API Level 33) and above.

caution

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
import PageSampleDesign from "generated/pages/pageSample";
import { Route, Router } from "@smartface/router";
import Application from "@smartface/native/application";
import File from "@smartface/native/io/file";
import Path from "@smartface/native/io/path";


//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myFile: File;

constructor(private router?: Router, private route?: Route) {
super({});
}

// The page design has been made from the code for better
// showcase purposes. As a best practice, remove this and
// use WYSIWYG editor to style your pages.
centerizeTheChildrenLayout() {
this.dispatch({
type: "updateUserStyle",
userStyle: {
flexProps: {
flexDirection: 'ROW',
justifyContent: 'CENTER',
alignItems: 'CENTER'
}
}
})
}

onShow() {
super.onShow();
const { headerBar } = this;
Application.statusBar.visible = false;
headerBar.visible = false;

}

onLoad() {
super.onLoad();
this.centerizeTheChildrenLayout();
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.myFile.size === destinationFile.size)
) {
this.myFile.copy(destinationFile.path);
}
}
}
}

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