javascript - fs.readFile 未读取整个文件
问题描述
我不完全确定为什么,但是当我执行最终displayLogFile()
函数时,该readFile()
函数不会显示整个文件。目前,所有错误都使用此代码正确记录到 log.log 文件中。
这段代码只是故意出错的 4 个不同的函数。在每个函数 catch 块中,错误被写入文件。我希望displayLogFile()
函数在写入所有错误后读取并显示此文件,但该readFile()
函数并未显示整个文件。
var fs = require("fs");
var logStream = fs.createWriteStream("./log.log"); //routes errors to log.log file
var writeConsole = new console.Console(logStream, logStream);
//#5
const displayLogFile = () => { //should display log.log file after all writing has completed.
console.log("This function is ran but readFileSync does not.");
JSON.stringify(
fs.readFile("./log.log", "utf-8", (err, file) => {
console.log(file); // This only console.log's: "Attempted to divide by zero." and is missing the other 3 logs from the file.
})
);
};
//#1
const divideByZero = () => { //testing error function
if (1 / 0 === Infinity) throw "Attempted to divide by zero.";
};
//#2
const fileDoesNotExist = () => { //testing error function
return fs.readFileSync("./NoFileNamedThis.txt", "utf-8");
};
//#3
const arrayOutOfBounds = () => { //testing error function
const arr = ["", "", ""];
if (arr[3] === undefined) throw "Index was outside the bounds of the array.";
};
//#4
const arrayIsNull = () => { //testing error function
var arr = ["", "", ""];
arr = null;
return arr[0];
};
//all of these try/catches error out on purpose
//#1
try {
divideByZero();
} catch (err) {
writeConsole.error(err);
}
//#2
try {
fileDoesNotExist();
} catch (err) {
writeConsole.error(err);
}
//#3
try {
arrayOutOfBounds();
} catch (err) {
writeConsole.error(err);
}
//#4
try {
arrayIsNull();
} catch (err) {
writeConsole.error(err);
}
logStream.close();
//#5
displayLogFile(); // this should display all of the error messages that have been written to the log.log file.
这是 log.log 文件,它在所有函数编写过程中都被很好地填充:
Attempted to divide by zero. <-- ERROR #1
Error: ENOENT: no such file or directory, open <-- ERROR #2 './NoFileNamedThis.txt'
at Object.openSync (fs.js:498:3)
at Object.readFileSync (fs.js:394:35)
at fileDoesNotExist (C:\Users:32:13)
at Object.<anonymous> (C:\Users:53:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: './NoFileNamedThis.txt'
}
Index was outside the bounds of the array. <-- ERROR #3
TypeError: Cannot read property '0' of null <-- ERROR #4
at arrayIsNull (C:\Users:43:13)
at Object.<anonymous> (C:\Users:65:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
解决方案
问题可能是使用readFileSync,根据文档https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options,它不使用异步 readFile 之类的回调,它要么在当前堆栈中引发错误,要么返回文件内容。
因此,您可以更改readFileSync调用:
const file = fs.readFileSync("./log.log", "utf-8")
console.log(file)
或使用fs.readFile:
fs.readFile("./log.log", "utf-8", (err, data) => {
if (err) throw err;
console.log(data);
});
正如@Pointy 在问题评论中指出的那样,在我的测试中,如果您在读取文件内容之前关闭 writeStream,它会起作用。我认为这个答案在fs.createWriteStream的行为中有更多细节
推荐阅读
- socket.io - Nifi 的 ConnectWebSocket 是否应该从 socket.io websocket 服务器获取消息?
- python - 如何使用线性回归预测值列表?
- java - 单个条目的 XSLT 转换为数组失败
- html - 如何编写脚本将许多值粘贴到 HTML 中的许多部分
- python - 无法写入 Sqlite3 db - Azure Web App
- jquery - 如果 LI 有孩子,则添加元素
- angular - 业力错误:“失败:无法读取未定义的属性‘订阅’”
- c++ - 将调用类转换为库本身后,库头文件中没有此类文件
- c - 如何获取 argv 并将其转换为 uint16_t
- html - 测试咖啡馆,选择子跨度有文本的按钮