import { Component , OnInit } from "@angular/core" ; import { ImageSource } from "@nativescript/core" ; import { knownFolders , path , File , Folder } from "@nativescript/core" ; @ Component ( { moduleId : module . id , templateUrl : "./read.component.html" } ) export class ReadExampleComponent implements OnInit { public folderName : string ; public fileName : string ; public fileTextContent : string ; public writtenContent : string ; public documents : Folder ; public folder : Folder ; public file : File ; public imageFile : File ; public binarySource : string ; public folderEntities : Array < FolderEntity > = [ ] ; ngOnInit ( ) { this . folderName = "NativeScript" ; this . fileName = "README.txt" ; this . fileTextContent = "Build amazing iOS and Android apps with technology you already know" + "Open source framework for building truly native mobile apps" + "with Angular, TypeScript or JavaScript." ; this . documents = knownFolders . documents ( ) ; this . folder = this . documents . getFolder ( this . folderName ) ; this . file = this . folder . getFile ( this . fileName ) ; this . file . writeText ( this . fileTextContent ) . then ( result => { this . file . readText ( ) . then ( res => { this . writtenContent = res ; } ) . catch ( err => { console . log ( err . stack ) ; } ) ; } ) . catch ( err => { console . log ( err ) ; } ) ; this . documents = knownFolders . documents ( ) ; this . documents . getEntities ( ) . then ( entities => { entities . forEach ( entity => { this . folderEntities . push ( new FolderEntity ( entity . name , entity . path , entity . lastModified . toString ( ) ) ) ; } ) ; } ) . catch ( err => { console . log ( err . stack ) ; } ) ; this . documents = knownFolders . documents ( ) ; let myPath = path . join ( this . documents . path , "Text.txt" ) ; let exists = File . exists ( myPath ) ; console . log ( "Does Text.txt exists: " + exists ) ; let temp = knownFolders . temp ( ) ; let tempExists = Folder . exists ( temp . path ) ; console . log ( "Does temp folder exists: " + tempExists ) ; } public onReadSync ( ) { let image = ImageSource . fromResourceSync ( "icon" ) ; let folder = knownFolders . documents ( ) ; let myPath = path . join ( folder . path , "Test.png" ) ; let saved = image . saveToFile ( myPath , "png" ) ; if ( saved ) { this . imageFile = File . fromPath ( myPath ) ; this . binarySource = this . imageFile . readSync ( err => { console . log ( "Error:" + err ) ; } ) ; console . log ( this . binarySource ) ; this . imageFile . writeSync ( this . binarySource , err => { console . log ( err ) ; } ) ; this . binarySource = "Successfully read binary data: " + this . binarySource ; } } } class FolderEntity { private name : string ; private myPath : string ; private lastModified : string ; constructor ( name , myPath , lastModified ) { this . name = name ; this . myPath = myPath ; this . lastModified = lastModified ; } }