FileStream

API Reference: IO.FileStream

FileStream is a class which allows you to making IO operations like reading, copying, creating or appending on file.

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.

Stream Types

Stream Type defines the stream behaviour that applied on stream object. There are three types of streams.

Write Write is the Stream Type that allows to write your content inside the file depending on Content Mode. For this type if file exists file content will be replaced with you data. File cannot be read with this type of streams.

Read Read is the Stream Type that allows to read your content inside the file depending on Content Mode. If file not exists you will get and exception. File cannot be written with this type of streams.

Append Append is the Stream Type that allows to append your content inside the file depending on Content Mode. Append works like write except if file exists append will write your data to end of the file so existing file will not be lost. File cannot be read with this type of streams.

For api reference of Stream Type, you can visit IO.FileStream.StreamType

Permission Management for FileStream

Managing permissions for FileStream the same as Permission Management for File Operations

Content Mode

Content Mode defines the stream behaviour that applied on stream object. Text mode is the default Content mode for File Streams. There are two modes of streams.

Binary Binary is the Content Mode that allows to open stream in binary mode.

Text Binary is the Content Mode that allows to open stream in text mode. It is good way to read document files.

For api reference of Content Mode, you can visit IO.FileStream.ContentMode

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import Label = require('sf-core/ui/label');
import Path = require('sf-core/io/path');
import File = require('sf-core/io/file');
import FileStream = require('sf-core/io/filestream');
import ImageView = require("sf-core/ui/imageview")
import Image = require("sf-core/ui/image")
import Http = require("sf-core/net/http");
import Button = require('sf-core/ui/button');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
buttonFont: Object = {
font: {
"size": 12,
"bold": true,
"italic": false,
"family": "SFProText",
"style": "Semibold"
},
};
http = new Http();
flexLayoutTopContainer: FlexLayout;
myLabel: Label;
myImageView: ImageView;
myFlexLayoutBottomContainer: FlexLayout;
myButtonAppendText: Button;
myButtonDownloadRandomImage: Button;
imageCounter: number = 0;
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.COLUMN;
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.flexLayoutTopContainer = new FlexLayout();
this.layout.addChild(this.flexLayoutTopContainer, "flexLayoutTopContainer", ".sf-flexLayout", {
width: null,
height: null,
flexGrow: 1,
flexProps: {
alignSelf: "STRETCH",
flexDirection: "ROW"
},
});
this.myLabel = new Label();
this.flexLayoutTopContainer.addChild(this.myLabel, "myLabel", ".sf-flexLayout", {
width: null,
height: null,
margin: 5,
flexGrow: 1,
multiline: true,
textColor: "#FFFFFF",
backgroundColor: "#00A1F1"
});
this.myImageView = new ImageView();
this.flexLayoutTopContainer.addChild(this.myImageView, "myImageView", ".sf-imageView", {
width: null,
height: null,
margin: 5,
flexGrow: 1,
backgroundColor: "#00A1F1",
imageFillType: "STRETCH",
flexProps: {
alignSelf: "STRETCH",
flexDirection: "ROW"
},
});
this.myFlexLayoutBottomContainer = new FlexLayout();
this.layout.addChild(this.myFlexLayoutBottomContainer, "myFlexLayoutBottomContainer", ".sf-flexLayout", {
width: null,
height: null,
margin: 5,
flexGrow: 2,
backgroundColor: "#00A1F1",
imageFillType: "STRETCH",
flexProps: {
flexWrap: "WRAP",
alignSelf: "STRETCH",
flexDirection: "ROW",
justifyContent: "SPACE_AROUND"
},
});
this.myButtonAppendText = new Button({
text: "Append Text",
onPress: () => {
let file = new File({ path: Path.DataDirectory + "/test.txt" });
if (!file.exists) {
file.createFile(true);
}
let fileStreamAppend = file.openStream(FileStream.StreamType.APPEND, FileStream.ContentMode.TEXT);
fileStreamAppend.write("Smartface Native Framework\n");
fileStreamAppend.close();
let fileStreamRead = file.openStream(FileStream.StreamType.READ, FileStream.ContentMode.TEXT);
this.myLabel.text = "File Content: \n" + fileStreamRead.readToEnd();
fileStreamRead.close();
}
});
this.myFlexLayoutBottomContainer.addChild(this.myButtonAppendText, "myButtonAppendText", ".sf-button", {
height: 75,
width: 150,
font: this.buttonFont
});
this.myButtonDownloadRandomImage = new Button({
text: "Download Random Image",
onPress: () => {
this.http.request({
url: "http://thecatapi.com/api/images/get?format=src",
method: "GET",
onLoad: (response) => {
let file = new File({ path: Path.DataDirectory + "/image-" + this.imageCounter + ".png" });
console.log("file:" + file)
if (!file.exists) {
file.createFile(true);
}
var fileStream = file.openStream(FileStream.StreamType.WRITE, FileStream.ContentMode.BINARY);
fileStream.write(response.body);
console.log("file is writed")
fileStream.close();
this.myImageView.image = Image.createFromFile(Path.DataDirectory + "/image-" + this.imageCounter + ".png");
this.imageCounter++;
},
onError: function (error) {
console.log("Download image failed");
}
})
}
});
this.myFlexLayoutBottomContainer.addChild(this.myButtonDownloadRandomImage, "myButtonDownloadRandomImage", ".sf-button", {
height: 75,
width: 150,
font: this.buttonFont
});
}
JavaScript
const Page = require('sf-core/ui/page');
const extend = require("js-base/core/extend");
const Color = require('sf-core/ui/color');
const Button = require('sf-core/ui/button');
const FlexLayout = require('sf-core/ui/flexlayout');
const Label = require('sf-core/ui/label');
const Path = require('sf-core/io/path');
const File = require('sf-core/io/file');
const FileStream = require('sf-core/io/filestream');
const Font = require('sf-core/ui/font');
const ImageView = require("sf-core/ui/imageview")
const Image = require("sf-core/ui/image")
const Http = require("sf-core/net/http");
var http = new Http();
var buttonFont = Font.create(Font.DEFAULT, 12, Font.NORMAL);
var page1 = extend(Page)(
function(_super) {
_super(this);
var flexLayoutTopContainer = new FlexLayout({
flexGrow: 1,
alignSelf: FlexLayout.AlignSelf.STRETCH,
flexDirection: FlexLayout.FlexDirection.ROW,
});
var myLabel = new Label({
margin: 5,
flexGrow: 1,
multiline: true,
textColor: Color.WHITE,
backgroundColor: Color.create("#00A1F1")
});
var myImageView = new ImageView({
margin: 5,
flexGrow: 1,
alignSelf: FlexLayout.AlignSelf.STRETCH,
backgroundColor: Color.create("#00A1F1"),
imageFillType: ImageView.FillType.STRETCH
});
flexLayoutTopContainer.addChild(myLabel);
flexLayoutTopContainer.addChild(myImageView);
var myFlexLayoutBottomContainer = new FlexLayout({
flexGrow: 2,
margin: 5,
alignSelf: FlexLayout.AlignSelf.STRETCH,
flexWrap: FlexLayout.FlexWrap.WRAP,
flexDirection: FlexLayout.FlexDirection.ROW,
justifyContent: FlexLayout.JustifyContent.SPACE_AROUND
});
var myButtonAppendText = new Button({
height: 75,
width: 150,
font: buttonFont,
text: "Append Text",
onPress: function() {
var file = new File({ path: Path.DataDirectory + "/test.txt" });
if (!file.exists) {
file.createFile(true);
}
var fileStreamAppend = file.openStream(FileStream.StreamType.APPEND, FileStream.ContentMode.TEXT);
fileStreamAppend.write("Smartface Native Framework\n");
fileStreamAppend.close();
var fileStreamRead = file.openStream(FileStream.StreamType.READ, FileStream.ContentMode.TEXT);
myLabel.text = "File Content: \n" + fileStreamRead.readToEnd();
fileStreamRead.close();
}
});
var imageCounter = 0;
var myButtonDownloadRandomImage = new Button({
height: 75,
width: 150,
font: buttonFont,
text: "Download Random Image",
onPress: function() {
console.log("hello")
http.request({
url: "http://thecatapi.com/api/images/get?format=src",
method: "GET",
onLoad: function(response) {
var file = new File({ path: Path.DataDirectory + "/image-" + imageCounter + ".png" });
console.log("file:" + file)
if (!file.exists) {
file.createFile(true);
}
var fileStream = file.openStream(FileStream.StreamType.WRITE, FileStream.ContentMode.BINARY);
fileStream.write(response.body);
console.log("file is writed")
fileStream.close();
myImageView.image = Image.createFromFile(Path.DataDirectory + "/image-" + imageCounter + ".png");
imageCounter++;
},
onError: function(error) {
console.log("Download image failed");
}
})
}
});
myFlexLayoutBottomContainer.addChild(myButtonAppendText);
myFlexLayoutBottomContainer.addChild(myButtonDownloadRandomImage);
this.layout.addChild(flexLayoutTopContainer);
this.layout.addChild(myFlexLayoutBottomContainer);
}
);
module.exports = page1;