const fs = require('fs');
// Создать, переименовать или переместить, прочитать содержимое, удалить директорию.
fs.mkdir('test-dir', 0o777, function (error) {if (error) {throw error;}
fs.rename('./test-dir', './other-dir', function (error) {if (error) {throw error;}
fs.readdir('./other-dir', {encoding: 'utf8'}, function (error, files) {if (error) {throw error;}
console.log(files);
fs.rmdir('./other-dir', function (error) {if (error) {throw error;}});
});
});
});
// Создать или записать, дописать, обрезать, прочитать содержимое, переименовать или переместить, скопировать, удалить файл.
fs.writeFile('test-file.txt', 'Hello world', {encoding: 'utf8', mode: 0o666, flag: 'w'}, function (error) {if (error) {throw error;}
fs.appendFile('./test-file.txt', ' and home', {encoding: 'utf8', mode: 0o666, flag: 'a'}, function (error) {if (error) {throw error;}
fs.truncate('./test-file.txt', 5, function (error) {if (error) {throw error;}
fs.readFile('./test-file.txt', {encoding: 'utf8', flag: 'r'}, function (error, data) {if (error) {throw error;}
console.log(data);
fs.rename('./test-file.txt', './other-file.txt', function (error) {if (error) {throw error;}
fs.link('./other-file.txt', './test-file.txt', function (error) {if (error) {throw error;}
fs.unlink('./other-file.txt', function (error) {if (error) {throw error;}
});
});
});
});
});
});
});
// Изменить права доступа, узнать время создания и изменения, изменить время создания и изменения, узнать полный путь файла.
fs.chmod('./test-file.txt', 0o777, function (error) {if (error) {throw error;}
fs.access('./test-file.txt', fs.constants.R_OK | fs.constants.W_OK, function (error) {
if (error) {console.log('No access!');} else {console.log('Can read | write');}
fs.stat('./test-file.txt', function (error, stat) {if (error) {throw error;}
console.log(stat.birthtime);
fs.utimes('./test-file.txt', (new Date().getTime() + 20000) / 1000, (new Date().getTime() + 50000) / 1000, function (error) {if (error) {throw error;}
fs.realpath('./test-file.txt', {encoding: 'utf8'}, function (error, resolvedPath) {if (error) {throw error;}
console.log(resolvedPath);
});
});
});
});
});
// Открыть, записать, закрыть файл.
fs.open('./test-file.txt', 'w', 0o666, function (error, fd) {if (error) {throw error;}
const dataBuffer = Buffer.from('abcdefghijklmnopqrstuvwxyz');
const dataBufferSize = dataBuffer.length;
let chunkBufferSize = 2; // in bytes
let chunkBuffer = dataBuffer.slice(0, chunkBufferSize);
let totalBytesWritten = 0;
const offset = 0;
function writeDataToFile () {
if (totalBytesWritten < dataBufferSize) {
fs.write(fd, chunkBuffer, offset, chunkBufferSize, totalBytesWritten, function (error, chunkBufferBytesWritten, buffer) {if (error) {throw error;}
totalBytesWritten += chunkBufferSize;
if ((totalBytesWritten + chunkBufferSize) > dataBufferSize) {
chunkBufferSize = dataBufferSize - totalBytesWritten;
}
chunkBuffer = dataBuffer.slice(totalBytesWritten, totalBytesWritten + chunkBufferSize);
writeDataToFile();
});
} else {
fs.close(fd, function (error) {if (error) {throw error;}
});
}
}
writeDataToFile();
});
// Открыть, прочитать, закрыть файл.
fs.open('./test-file.txt', 'r', function (error, fd) {if (error) {throw error;}
fs.fstat(fd, function (error, stat) {if (error) {throw error;}
const fileSize = stat.size;
let fileBuffer = Buffer.alloc(fileSize);
let chunkBufferSize = 2; // in bytes
let totalBytesRead = 0;
function readDataFromFile () {
if (totalBytesRead < fileSize) {
if ((totalBytesRead + chunkBufferSize) > fileSize) {
chunkBufferSize = fileSize - totalBytesRead;
}
fs.read(fd, fileBuffer, totalBytesRead, chunkBufferSize, totalBytesRead, function (error, chunkBytesRead, buffer) {if (error) {throw error;}
totalBytesRead += chunkBufferSize;
console.log(buffer.toString('utf8'));
readDataFromFile();
});
} else {
fs.close(fd, function (error) {if (error) {throw error;}
});
}
}
readDataFromFile();
});
});
// Поток записи в файл.
const writeStream = fs.createWriteStream('./test-file.txt', {encoding: 'utf8', mode: 0o666, flags: 'w', fd: null, autoClose: true});
writeStream.on('error', function (error) {throw error;});
writeStream.on('open', function () {console.log('writeFile opened');});
writeStream.on('close', function () {
console.log('bytesWritten: ' + writeStream.bytesWritten + ' | path: ' + writeStream.path);
console.log('writeFile closed');
});
writeStream.write('some');
writeStream.write(' data');
writeStream.end();
// Поток чтения из файла.
const readStream = fs.createReadStream('./test-file.txt', {encoding: 'utf8', mode: 0o666, flags: 'r', fd: null, autoClose: true});
readStream.on('error', function (error) {throw error;});
readStream.on('open', function () {console.log('readStream open');});
readStream.on('data', function (data) {console.log(data + ' | bytesRead: ' + readStream.bytesRead + ' | path: ' + readStream.path);});
readStream.on('end', function (data) {console.log(data + ' | readStream end');});
readStream.on('close', function () {console.log('readStream closed');});
// Отслеживать изменения в файле.
// Первый способ.
function watchHandler (current, previous) {
console.log('Current mtime:' + current.mtime + ' | Previous mtime: ' + previous.mtime);
}
fs.watchFile('./test-file.txt', {persistent: true, interval: 5007}, watchHandler);
fs.unwatchFile('./test-file.txt', watchHandler);
// Второй способ.
const watcher = fs.watch('./test-file.txt', {encoding: 'utf8', persistent: true, recursive: true});
watcher.on('error', function (error) {throw error;});
watcher.on('change', function (eventType, filename) {
console.log(eventType);
if (filename) {console.log(filename);}
watcher.close();
});
Комментариев нет:
Отправить комментарий