FileStream

API Reference: IO.FileStream

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

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

JavaScript
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;