节点版本:v12.16.1os:Windows 7
最近,我尝试使用fs.readFile()访问由fs.createWriteStream()创建的日志文件,但仅获取它的第一行。但是,当我创建具有相同内容的测试文件并使用fs.readFile()来获取它时,它就可以正常工作。仅仅是因为第一个文件是由fs.createWriteStream()创建的吗?我也对此进行了一些测试:
//create a writeable stream
const fs = require('fs');
let output = fs.createWriteStream('./output.txt');
output.write('c1:1');
output.write('c1:2');
output.write('default:1');
//read the file synchronously
let read = fs.readFileSync('./output.txt','utf8');
console.log(`read the output file synchronously: \n${read}`);
//read the test file synchronously
let readTest = fs.readFileSync('./test.txt','utf8');
console.log(`read the test file synchronously: \n${readTest}`);
//read the file asynchronously
let readAsync = fs.readFile('./output.txt','utf8',(err,data)=>{
console.log(`read the file asynchronously: \n${data}`);
});
//read the test file asynchronously
let readTestAsync = fs.readFile('./test.txt','utf8',(err,data)=>{
console.log(`read the test file asynchronously: \n${data}`);
});
//read the file with fs.createReadStream
let stream = fs.createReadStream('./output.txt','utf8');
let readWithStream = "";
stream.on('data',(data)=>{
readWithStream += data;
});
stream.on('end',(data)=>{
console.log(`read the file with fs.createReadStream : \n${readWithStream}`);
});
//read the test file with fs.createReadStream
let streamTest = fs.createReadStream('./test.txt','utf8');
let readTestWithStream = "";
streamTest.on('data',(data)=>{
readTestWithStream += data;
});
streamTest.on('end',(data)=>{
console.log(`read the test file with fs.createReadStream : \n${readTestWithStream}`);
});
这是结果:
具有相同问题的朋友的新版本:
//create a writeable stream for our Console instance as stdout
const fs = require('fs');
let output = fs.createWriteStream('./output.txt');
output.write('c1:1\n');
output.write('c1:2\n');
output.write('default:1\n');
output.end(); //close the writable stream
setTimeout(readAfter,5000); //5s delay to ensure all the data has been writing into the disk and then read it
function readAfter(){
//read the file synchronously
let read = fs.readFileSync('./output.txt','utf8');
console.log(`read the output file synchronously: \n${read}`);
//read the test file synchronously
let readTest = fs.readFileSync('./test.txt','utf8');
console.log(`read the test file synchronously: \n${readTest}`);
//read the file asynchronously
let readAsync = fs.readFile('./output.txt','utf8',(err,data)=>{
console.log(`read the file asynchronously: \n${data}`);
});
//read the test file asynchronously
let readTestAsync = fs.readFile('./test.txt','utf8',(err,data)=>{
console.log(`read the test file asynchronously: \n${data}`);
});
//read the file with fs.createReadStream
let stream = fs.createReadStream('./output.txt','utf8');
let readWithStream = "";
stream.on('data',(data)=>{
readWithStream += data;
});
stream.on('end',(data)=>{
console.log(`read the file with fs.createReadStream : \n${readWithStream}`);
});
//read the test file with fs.createReadStream
let streamTest = fs.createReadStream('./test.txt','utf8');
let readTestWithStream = "";
streamTest.on('data',(data)=>{
readTestWithStream += data;
});
streamTest.on('end',(data)=>{
console.log(`read the test file with fs.createReadStream : \n${readTestWithStream}`);
});
};