function foo(){
var filename="xxx.txt";
var readtxt;
var file = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("Home", Ci.nsIFile);
file.append(filename);
NetUtil.asyncFetch(file, function(inputStream, status) {
if (!components.isSuccessCode(status)) {
// Handle error!
console.log("file read error.."+filename);
return;
}
// The file data is contained within inputStream.
// You can read it into a string with
readtxt = NetUtil.readInputStreamToString(inputStream, inputStream.available());
console.log("firstly print="+readtxt);
});
console.log("secondly print="+readtxt);
}
The purpost of the code is to read from a file and then print it.
In the above code, I define a var readtxt
. Then in line console.log("firstly print="+readtxt);
, readtxt
is successfully printed. However, in line console.log("secondly print="+readtxt);
it says secondly print=undefined
. I don't know why.