Viết vào một tệp là một trong các tác vụ lập trình cơ bản mà người ta thường cần biết - may mắn thay, nhiệm vụ này rất đơn giản trong Node.js. Chúng ta có thể sử dụng phương thức
3 tiện dụng bên trong mô -đun fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
4 của thư viện tiêu chuẩn, có thể tiết kiệm tất cả các loại thời gian và rắc rối.fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
fs = require['fs'];
fs.writeFile[filename, data, [encoding], [callback]]
fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
5 FILEPATH CỦA FILE để ghifs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
6 Dữ liệu bạn muốn ghi vào tệpfs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
7 Mã hóa fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
8. Mã hóa có thể là 'ASCII', 'UTF8' và 'Base64'. Nếu không có mã hóa được cung cấp, thì 'UTF8' được giả định.fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
9 Nếu không có lỗi, [contents of helloworld.txt]:
Hello World!
0, nếu không [contents of helloworld.txt]:
Hello World!
1 chứa thông báo lỗi.Vì vậy, nếu chúng ta muốn viết "Hello World" thành
[contents of helloworld.txt]:
Hello World!
2:fs = require['fs'];
fs.writeFile['helloworld.txt', 'Hello World!', function [err] {
if [err] return console.log[err];
console.log['Hello World > helloworld.txt'];
}];
[contents of helloworld.txt]:
Hello World!
Nếu chúng tôi cố tình muốn gây ra lỗi, chúng tôi có thể cố gắng ghi vào một tệp mà chúng tôi không có quyền truy cập:
fs = require['fs']
fs.writeFile['/etc/doesntexist', 'abc', function [err,data] {
if [err] {
return console.log[err];
}
console.log[data];
}];
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
Bạn có thể sửa đổi mặc định bằng cách chỉ định cờ:
3][contents of helloworld.txt]: Hello World!
Những lá cờ bạn có thể sử dụng là
4[contents of helloworld.txt]: Hello World!
Lá cờ
5][contents of helloworld.txt]: Hello World!
Sự mô tả
Tệp được tạo nếu nó không tồn tại
1 “A buffer” is the object. Whether or not it “is buffered” is a property of that object.{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
Cờ này mở tệp để đọc và viết
❌
write["a"]
write["b"]
write["c"]
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
2✅
Tính đến thời điểm hiện tại, trong Node.js v0.12 [phiên bản ổn định được công bố 02/06/2015] hiện hỗ trợ hai chức năng:
fs = require['fs']
fs.writeFile['/etc/doesntexist', 'abc', function [err,data] {
if [err] {
return console.log[err];
}
console.log[data];
}];
3 và fs = require['fs']
fs.writeFile['/etc/doesntexist', 'abc', function [err,data] {
if [err] {
return console.log[err];
}
console.log[data];
}];
4. Có vẻ như các chức năng này cuối cùng sẽ cho phép bạn đệm/xóa các cuộc gọi ghi.Ví dụ, trong Java có một số lớp cung cấp các luồng đệm [
fs = require['fs']
fs.writeFile['/etc/doesntexist', 'abc', function [err,data] {
if [err] {
return console.log[err];
}
console.log[data];
}];
5, ________ 36 ...]. Nếu bạn viết ba byte, các byte này sẽ được lưu trữ trong bộ đệm [bộ nhớ] thay vì thực hiện cuộc gọi I/O chỉ cho ba byte. Khi bộ đệm đầy đủ, nội dung được xóa và lưu vào đĩa. Điều này cải thiện hiệu suất.Tôi không khám phá ra bất cứ điều gì, chỉ cần nhớ cách truy cập đĩa.
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
5 [và đối tác { stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
6 của nó]:- Ví dụ với những lời hứa
- Đây là một ví dụ
7:{ stack: [Getter/Setter], arguments: undefined, type: undefined, message: 'EACCES, Permission denied \'/etc/doesntexist\'', errno: 13, code: 'EACCES', path: '/etc/doesntexist' }
- Đây là một ví dụ
- Cờ này mở tệp để viết và nó cũng định vị luồng ở cuối tệp
- Cờ này mở tệp để đọc và ghi và nó cũng định vị luồng ở cuối tệp
Ví dụ với những lời hứa
Đây là một ví dụ
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
7:JS
Đây là một ví dụ { stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
7:
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
Chỉnh sửa trang này trên github
JS
Mục lục
JS
Vượt qua một tập tinreplace the contents of the file if it does already exist.
Bạn có thể sửa đổi mặc định bằng cách chỉ định cờ:
JS
Những lá cờ bạn có thể sử dụng là
1 | Cờ này mở tệp để đọc và viếtreading and writing | ❌ |
2 | Cờ này mở tệp để đọc và ghi và nó cũng định vị luồng ở đầu tệpreading and writing and it also positions the stream at the beginning of the file | ✅ |
3 | Cờ này mở tệp để viết và nó cũng định vị luồng ở cuối tệpwriting and it also positions the stream at the end of the file | ✅ |
4 | Cờ này mở tệp để đọc và ghi và nó cũng định vị luồng ở cuối tệpreading and writing and it also positions the stream at the end of the file | ✅ |
- Bạn có thể tìm thêm thông tin về các cờ trong tài liệu FS.
Nối nội dung vào một tệp
Lối vào các tệp rất tiện dụng khi bạn không muốn ghi đè lên nội dung mới, nhưng thay vào đó thêm vào nó.
Ví dụ
Một phương thức tiện dụng để nối nội dung vào cuối tệp là
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
5 [và đối tác { stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
6 của nó]:JS
Ví dụ với những lời hứa
Đây là một ví dụ
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'EACCES, Permission denied \'/etc/doesntexist\'',
errno: 13,
code: 'EACCES',
path: '/etc/doesntexist' }
7:JS
Chỉnh sửa trang này trên github