文件操作
文件读取
ts
import fs from "fs";
const data = fs.readFileSync("example/file-1.txt", "utf8");
console.log(data);
fs.readFile("example/file-2.txt", "utf8", (err, data) => {
if (err) throw err;
console.log(data);
});
文件写入
ts
import fs from "fs";
fs.writeFileSync("example/file-1.txt", "Hello, World!", "utf8");
fs.writeFile("example/file-2.txt", "Hello, World!", "utf8", (err) => {
if (err) throw err;
console.log("File written successfully");
});
文件追加
ts
import fs from "fs";
fs.appendFileSync("example/file-1.txt", "Hello, Javascript!", "utf8");
fs.appendFile("example/file-2.txt", "Hello, Javascript!", "utf8", (err) => {
if (err) throw err;
console.log("Data appended successfully");
});
文件删除
ts
import fs from "fs";
fs.unlinkSync("example/file-1.txt");
fs.unlink("example/file-2.txt", (err) => {
if (err) throw err;
console.log("File deleted successfully");
});