Importing a file in Javascript using Ubuntu
how do I import a file in Javascript on Ubuntu? Here is my current code:
TextFile.js
var file = null;
function createFile()
{
file = new StreamReader();
file.open("~//home//chris//LcdDisplayFile//qml//file.txt");
}
function readLine()
{
var str = file.readLine();
return str;
}
For some reason it cannot find the file. I am new to both jscript and ubuntu. Error:
file:///home/chris/LcdDisplayFile/qml/TextFile.js:10: TypeError: Result of expression 'fi开发者_Python百科le' [null] is not an object.
~
already points to /home/chris/
. Also, why did you use a double //
?[1] These slashes have no special meaning inside a string.
[1]Windows directories are separated by a backslash (\
), which have a special meaning in JavaScript strings (escape character), hence \\
. Normal slashes do not have to be escaped, and can be safely written as /
(One exception: Regular expressions)
Don't know anything about QML, I assume it supports IO and provides it own class for reading streams and that you are using it correctly. Either way your file path is invalid... shouldn't it just be:
var path = '~/LcdDisplayFile/qml/file.txt'
or
var path = '/home/chris/LcdDisplayFile/qml/file.txt'
Also keep in mine Linux file paths are case-sensitive.
Unless you are using a non-standard server-side extension of JavaScript such as NodeJS ( http://nodejs.org/docs/v0.5.9/api/fs.html#fs.open ), file I/O is not part of the standard set of objects. JavaScript in the browser does not support file I/O.
At any rate, the main problem here is, what and where is the StreamReader object. If the given JavaScript environment provides it, then the object should not be a null.
Here's some documentation regarding using javascript with QML. I hope this helps:
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-javascript-imports.html
精彩评论