Hướng dẫn nodejs write string to file - nodejs ghi chuỗi vào tệp

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

fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
  if (err) return console.log(err);
  console.log('Hello World > helloworld.txt');
});
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(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 để ghi

fs = 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ệp

fs = 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ờ:

  1. [contents of helloworld.txt]:
    Hello World!
    
    3)

    Những lá cờ bạn có thể sử dụng là

  2. [contents of helloworld.txt]:
    Hello World!
    
    4

    Lá cờ

  3. [contents of helloworld.txt]:
    Hello World!
    
    5)

    Sự mô tả

Tệp được tạo nếu nó không tồn tại

{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'EACCES, Permission denied \'/etc/doesntexist\'',
  errno: 13,
  code: 'EACCES',
  path: '/etc/doesntexist' }
1 “A buffer” is the object. Whether or not it “is buffered” is a property of that object.

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

Cờ này mở tệp để đọc và ghi và nó cũng định vị luồng ở đầu tệp

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.

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ó):
  • 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:
  • 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:

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à

Lá cờSự mô tảTệp được tạo nếu nó không tồn tại
{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'EACCES, Permission denied \'/etc/doesntexist\'',
  errno: 13,
  code: 'EACCES',
  path: '/etc/doesntexist' }
1
Cờ này mở tệp để đọc và viếtreading and writing
{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'EACCES, Permission denied \'/etc/doesntexist\'',
  errno: 13,
  code: 'EACCES',
  path: '/etc/doesntexist' }
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
{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'EACCES, Permission denied \'/etc/doesntexist\'',
  errno: 13,
  code: 'EACCES',
  path: '/etc/doesntexist' }
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
{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'EACCES, Permission denied \'/etc/doesntexist\'',
  errno: 13,
  code: 'EACCES',
  path: '/etc/doesntexist' }
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