Table of contents
fs.exists() Determines whether a file or directory exists and returns a bool value
fs.readdir() reads all files and subdirectories in a directory
compressing compresses and decompresses files
Unzip the file with Chinese name
fs read operation file
Introduction and Notes
introduce
const fs = require("fs");
Precautions
1 Adding the suffix sync is a synchronous operation example (fs.writeFileSync), otherwise it is an asynchronous example (fs.writeFile)
2 The first parameter of the callback function must be an error object error If an error occurs, the error will be returned through the first parameter error
3 The second parameter is the data of a successful response. If there is no exception, error will be set to null
increase
fs.writeFile() override
Create a file, if the file exists, write data to overwrite the original data, if it does not exist, create and write, if the directory where the file is located does not exist, an error will be reported, only files can be created, folders cannot be created
fs.writeFile(
"../新建文件夹/新建文本文档.txt",
"新增的数据",
function (err,data) {
console.log(err,data);
}
);
add fs.appendFile()
Create a file, if the file exists, append information after the original data, if it does not exist, create and write
fs.appendFile(
"../新建文件夹/新建文本文档.txt",
"新增的数据",
function (err,data) {
console.log(err,data);
}
);
fs.mkdir() folder
Create a folder, create it if the directory does not exist, and report an error if it exists
fs.mkdir(
"../新建文件夹",
function (err,data) {
console.log(err,data);
}
);
delete
fs.unlink() deletes a file
fs.unlink(
"../新建文件夹/新建文本文档.txt",
function (err,data) {
console.log(err,data);
}
);
fs.rmdir() delete folder
Must be an empty directory to delete, otherwise an error will be reported
fs.rmdir(
"../新建文件夹",
function (err,data) {
console.log(err,data);
}
);
change
fs.rename()
You can modify the location, file name, and suffix of the file, and be careful not to cross disks
fs.rename(
"../新建文件夹/新建文本文档.txt",
"../新建文件夹/新建.qax",
function (err,data) {
console.log(err,data);
}
);
check
fs.exists() Determines whether a file or directory exists and returns a bool value
fs.exists(
"../新建文件夹/新建文本文档.txt",
function (err,data) {
console.log(err,data);
}
);
fs.readdir() reads all files and subdirectories in a directory
withFileTypes can determine the type (file 1 directory 2) The first parameter of the callback is the error message, and the second parameter is an array containing the sub-file names
fs.readdir(
"../新建文件夹",
{withFileTypes:true},
function (err,data) {
console.log(err,data);
}
);
fs.readFile() reads a file
The second parameter sets the encoding method, otherwise the default binary stream buffer
fs.readFile(
"../新建文件夹/新建文本文档.txt",
"utf8",
function (err,data) {
console.log(err,data);
}
);
compressing compresses and decompresses files
Installation and reference
Install
npm install compressing
introduce
const compressing = require("compressing");
Supported formats: tar, gzip, tgz, zip
use
Compress a single file
compressing.zip.compressFile('uploads/test.txt', 'uploads/test.zip').then(() => {}).catch(() => {});
compressed folder
compressing.zip.compressDir('./uploads/test', 'test3.zip').then(()=>{}).catch(()=>{});
unzip files
compressing.zip.uncompress('./uploads/test3.zip', './uploads/test3').then(() => {}).catch(() => {});
Unzip the file with Chinese name
compressing.zip.uncompress("../新建文件夹.zip", "../新建文件夹", {zipFileNameEncoding: "GBK",}).then(() => {}).catch(() => {});
Open Folder vs. Open File
open folder
this.$electron.remote.shell.showItemInFolder('绝对路径');
open a file
The path module of nodeJS needs to be introduced
import path from "path";
open a file
this.$electron.remote.shell.openExternal(path.resolve("绝对路径"));