Hi,
I'm using a shared folder in my app where I have some files to reuse. I need to read file contents from that directory. I'm having the following:
var fs = require( 'file-system' );
var app_files = fs.knownFolders.currentApp();
var path = fs.path.join( app_files.path, 'shared/i18n/dictionaries', 'en-US.json' );
if ( fs.File.exists( path ) ) {
console.log( 'file exists' ); // I get this log
console.log( app_files.getFile( path ).readTextSync() ); // I get ""
}
I couldn't find similar issue.
Occurs on both platforms.
tns version: 2.4.0
tns core modules version: 2.4.1
Please advise.
Hey @ickata thank you for reporting this one - I have tested it on my side and can confirm that indeed if we have a file with content that is not created by file.writeText() then the content is not accessible and methods readText() and readTextSync(0 are always returning an empty string. We will post additional info here. meanwhile, as a temporary solution, you can create/override your file content with writeText() and then you will be able to receive the content with _readTextSync()_ or _readText()_ methods.
Hi @NickIliev ,
Thanks for the quick response. Since I'm using JSON files, a simple require did the job and solved my problem. But still, it will be great to have readTextSync read other files as well :)
Hey @ickata the thing is that path join creates absolute path like this
var appFiles = fs.knownFolders.currentApp();
var path = fs.path.join(appFiles.path, 'shared/i18n/dictionaries/', 'test.json');
console.log(path);
//returns: /data/data/org.nativescript.fileSystem/files/app/shared/i18n/dictionaries/test.json
and in the same time readText and readTextSync methods are reading the path relative to the app directory (as we are in currentApp directory) .That said to using the relative path lin this case like this
var appFiles = fs.knownFolders.currentApp(); // this is /data/data/org.nativescript.fileSystem/files/app/
var file = appFiles.getFile("shared/i18n/dictionaries/test.json");
var content = file.readTextSync();
console.log("content: " + content);
I have file already stored on the device and I wanna read it how I will override it if I cant read his content ??? @NickIliev
@elmissouri16 not sure I am following on the above - can you please prepare a demo application of what you are trying to achieve or provide more detailed explanation (including code snippets)
It appears that readText will only read existing files in the assets directory. I was stying to read a .css file I had placed in the app directory and I kept getting an empty string. Moving the file to app/assets solved my problem.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hey @ickata the thing is that path join creates absolute path like this
and in the same time readText and readTextSync methods are reading the path relative to the app directory (as we are in currentApp directory) .That said to using the relative path lin this case like this