nodejs内蔵モジュールfsの使用
const fs=require('fs')
// fs.stat
fs.stat('.html',(err,data)=>{
if(err){
console.log(err);
return;
}
console.log(` :${data.isFile()}`)
console.log(` :${data.isDirectory()}`)
})
/*
* path
* mode ( ), 777
* callback , err
*/
fs.mkdir('./css',(err)=>{ // css
if(err){
console.log(err)
return;
}
console.log(' ')
})
// , ,
fs.writeFile('./html/index.html',' nodejs',(err)=>{
if(err){
console.log(err)
return
}
console.log(' ')
})
// , , ,
//
fs.appendFile('./css/base.css','body{color:red}
',(err)=>{
if(err){
console.log(err)
return
}
console.log('appendFile ')
})
fs.readFile('./html/index.html',(err,data)=>{
if(err){
console.log(err)
return
}
console.log(data) // Buffer
console.log(data.toString()); // Buffer String
})
// html ,
fs.readdir('./html',(err,data)=>{
if(err){
console.log(err)
return
}
console.log(data) // ['index.html','js'] js
})
/*
* parma1
* param2
* param3
*/
fs.rename('./html/aa.html','./html/home.html',(err)=>{
if(err){
console.log(err)
return
}
console.log(' ')
})
//
fs.rename('./html/home.html','./css/home.html',(err)=>{
if(err){
console.log(err)
return
}
console.log(' ')
})
// aa , fs.unlink
fs.rmdir('./aa',(err)=>{
if(err){
console.log(err)
return
}
console.log(' ')
})
fs.unlink('./aa/index.html',(err)=>{
if(err){
console.log(err)
return
}
console.log(' ')
})
const fs = require('fs');
var fileReadStream = fs.createReadStream('data.json');
let count = 0;
var str = '';
fileReadStream.on('data',(chunk)=>{
console.log(`${++count} :${chunk.length}`)
str +=chunk;
})
fileReadStream.on('end',() => {
console.log('--- ---');
console.log(count);
console.log(str);
})
fileReadStream.on('error', (error) => {
console.log(error)
})
var fs = require("fs");
var data = ' , ';
// , output.txt
var writerStream = fs.createWriteStream('output.txt'); // utf8 writerStream.write(data,'UTF8');
//
writerStream.end();
// --> finish writerStream.on('finish', function() {
console.log(" 。"); });
writerStream.on('error', function(err){ console.log(err.stack);
}); console.log(" ");