Nodejs đọc tệp và gửi phản hồi

Tệp I/O được cung cấp bởi các trình bao bọc đơn giản xung quanh các chức năng POSIX tiêu chuẩn. Để sử dụng mô-đun này, hãy làm

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
6. Tất cả các phương thức đều có dạng không đồng bộ và đồng bộ

Show

Biểu mẫu không đồng bộ luôn lấy lệnh gọi lại hoàn thành làm đối số cuối cùng của nó. Các đối số được truyền cho lệnh gọi lại hoàn thành phụ thuộc vào phương thức, nhưng đối số đầu tiên luôn được dành riêng cho một ngoại lệ. Nếu thao tác được hoàn thành thành công, thì đối số đầu tiên sẽ là

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
7 hoặc
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

Khi sử dụng biểu mẫu đồng bộ, mọi ngoại lệ sẽ được đưa ra ngay lập tức. Bạn có thể sử dụng try/catch để xử lý các ngoại lệ hoặc cho phép chúng nổi lên

Đây là một ví dụ về phiên bản không đồng bộ

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});

Đây là phiên bản đồng bộ

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');

Với các phương thức không đồng bộ, không có thứ tự được đảm bảo. Vì vậy, những điều sau đây dễ bị lỗi

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});

Có thể là

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
9 được thực thi trước
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
0. Cách chính xác để làm điều này là xâu chuỗi các cuộc gọi lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});

Trong các quy trình bận rộn, lập trình viên được khuyến khích sử dụng các phiên bản không đồng bộ của các cuộc gọi này. Các phiên bản đồng bộ sẽ chặn toàn bộ quá trình cho đến khi chúng hoàn tất — tạm dừng tất cả các kết nối

Đường dẫn tương đối đến tên tệp có thể được sử dụng. Tuy nhiên, hãy nhớ rằng đường dẫn này sẽ liên quan đến

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
1

Hầu hết các hàm fs cho phép bạn bỏ qua đối số gọi lại. Nếu bạn làm như vậy, một lệnh gọi lại mặc định sẽ được sử dụng để hiển thị lại các lỗi. Để có dấu vết đến trang cuộc gọi ban đầu, hãy đặt biến môi trường

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
2

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    

Bộ đệm API #

Các hàm

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
3 hỗ trợ chuyển và nhận đường dẫn dưới dạng cả chuỗi và Bộ đệm. Cái sau nhằm mục đích làm cho nó có thể hoạt động với các hệ thống tệp cho phép tên tệp không phải UTF-8. Đối với hầu hết các mục đích sử dụng thông thường, việc làm việc với các đường dẫn dưới dạng Bộ đệm sẽ không cần thiết vì chuỗi API tự động chuyển đổi sang và từ UTF-8

Lưu ý rằng trên một số hệ thống tệp nhất định (chẳng hạn như NTFS và HFS+), tên tệp sẽ luôn được mã hóa dưới dạng UTF-8. Trên các hệ thống tệp như vậy, việc chuyển Bộ đệm được mã hóa không phải UTF-8 sang các hàm

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
3 sẽ không hoạt động như mong đợi

Lớp. fs. FSWatcher#

Các đối tượng được trả về từ

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5 thuộc loại này

Cuộc gọi lại

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
6 được cung cấp cho
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5 nhận các sự kiện
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
8 của FSWatcher được trả về

Đối tượng tự phát ra các sự kiện này

Biến cố. 'biến đổi'#

Phát ra khi có gì đó thay đổi trong thư mục hoặc tệp đã xem. Xem thêm chi tiết trong

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5

Đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90 có thể không được cung cấp tùy thuộc vào sự hỗ trợ của hệ điều hành. Nếu cung cấp
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90, nó sẽ được cung cấp dưới dạng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92 nếu
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5 được gọi với tùy chọn
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 của nó được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, nếu không thì
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90 sẽ là một chuỗi

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
9

Biến cố. 'lỗi'#

Phát ra khi xảy ra lỗi

người quan sát. đóng lại()#

Ngừng theo dõi các thay đổi trên

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
97 đã cho

Lớp. fs. ReadStream#

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
98 là một Luồng có thể đọc được

Biến cố. 'mở'#

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99 Bộ mô tả tệp số nguyên được sử dụng bởi ReadStream

Được phát ra khi tệp của ReadStream được mở

Biến cố. 'đóng lại'#

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99 Bộ mô tả tệp số nguyên được sử dụng bởi ReadStream

Được phát ra khi bộ mô tả tệp cơ bản của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
98 đã bị đóng

readStream. byte đã đọc #

Số lượng byte đã đọc cho đến nay

readStream. con đường#

Đường dẫn đến tệp mà luồng đang đọc từ đó như được chỉ định trong đối số đầu tiên của

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
22. Nếu
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 được truyền dưới dạng chuỗi, thì
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
24 sẽ là chuỗi. Nếu
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 được chuyển thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92, thì
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
24 sẽ là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

Lớp. fs. Số liệu thống kê #

Các đối tượng được trả về từ

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
29,
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
00 và
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
01 và các đối tượng đồng bộ của chúng thuộc loại này

  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    02
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    03
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    04
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    05
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    06 (chỉ hợp lệ với
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    00)
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    08
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    09

Đối với một tệp thông thường,

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
00 sẽ trả về một chuỗi rất giống với chuỗi này

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
2

Xin lưu ý rằng ________ 501, ________ 502, ________ 503 và ________ 504 là các thể hiện của đối tượng

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
05 và để so sánh giá trị của các đối tượng này, bạn nên sử dụng các phương pháp thích hợp. Đối với hầu hết các mục đích sử dụng chung,
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
06 sẽ trả về số mili giây đã trôi qua kể từ ngày 1 tháng 1 năm 1970 00. 00. 00 UTC và số nguyên này phải đủ để so sánh, tuy nhiên, có các phương pháp bổ sung có thể được sử dụng để hiển thị thông tin mờ. Bạn có thể tìm thêm chi tiết trong trang Tham khảo JavaScript MDN

Giá trị thời gian thống kê

Thời gian trong đối tượng stat có ngữ nghĩa sau

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    01 "Thời gian truy cập" - Thời gian khi dữ liệu tệp được truy cập lần cuối. Được thay đổi bởi các lệnh gọi hệ thống mknod(2), utimes(2) và read(2)
  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    02 "Thời gian sửa đổi" - Thời gian khi dữ liệu tệp được sửa đổi lần cuối. Được thay đổi bởi các lệnh gọi hệ thống mknod(2), utimes(2) và write(2)
  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    04 "Thời gian thay đổi" - Thời gian khi trạng thái tệp được thay đổi lần cuối (sửa đổi dữ liệu inode). Được thay đổi bởi lệnh gọi hệ thống chmod(2), chown(2), link(2), mknod(2), rename(2), unlink(2), utimes(2), read(2) và write(2)
  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    03 "Birth Time" - Thời gian tạo tập tin. Đặt một lần khi tệp được tạo. Trên các hệ thống tệp không có sẵn thời gian sinh, thay vào đó, trường này có thể chứa
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    04 hoặc
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    32 (tức là dấu thời gian unix epoch
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    33). Lưu ý rằng giá trị này có thể lớn hơn
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    01 hoặc
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    02 trong trường hợp này. Trên Darwin và các biến thể FreeBSD khác, cũng được đặt nếu
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    01 được đặt rõ ràng thành giá trị sớm hơn
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    03 hiện tại bằng cách sử dụng lệnh gọi hệ thống utimes(2)

Trước nút v0. 12,

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
04 giữ
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
03 trên hệ thống Windows. Lưu ý rằng kể từ v0. 12,
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
04 không phải là "thời gian sáng tạo" và trên các hệ thống Unix, nó chưa bao giờ

Lớp. fs. GhiStream#

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
01 là Luồng có thể ghi

Biến cố. 'mở'#

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99 Bộ mô tả tệp số nguyên được sử dụng bởi WriteStream

Được phát ra khi tệp của WriteStream được mở

Biến cố. 'đóng lại'#

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99 Bộ mô tả tệp số nguyên được sử dụng bởi WriteStream

Được phát ra khi bộ mô tả tệp cơ bản của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
01 đã bị đóng

writeStream. byteWritten#

Số lượng byte được ghi cho đến nay. Không bao gồm dữ liệu vẫn đang xếp hàng để ghi

writeStream. con đường#

Đường dẫn đến tệp mà luồng đang ghi vào như được chỉ định trong đối số đầu tiên của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
05. Nếu
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 được truyền dưới dạng chuỗi, thì
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
07 sẽ là chuỗi. Nếu
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 được chuyển thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92, thì
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
07 sẽ là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. truy cập (đường dẫn [, chế độ], gọi lại) #

Kiểm tra quyền của người dùng đối với tệp hoặc thư mục được chỉ định bởi

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23. Đối số
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
13 là một số nguyên tùy chọn chỉ định kiểm tra khả năng truy cập sẽ được thực hiện. Các hằng số sau xác định các giá trị có thể có của
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
13. Có thể tạo mặt nạ bao gồm OR theo bit của hai hoặc nhiều giá trị

  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    15 -
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23 hiển thị đối với quy trình gọi. Điều này hữu ích để xác định xem một tệp có tồn tại hay không, nhưng không nói gì về quyền của
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    17. Mặc định nếu không có
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    13 được chỉ định
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    19 -
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23 có thể được đọc bởi quá trình gọi
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    21 -
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23 có thể được viết bởi quá trình gọi
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    23 -
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23 có thể được thực hiện bởi quá trình gọi. Điều này không ảnh hưởng đến Windows (sẽ hoạt động như
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    15)

Đối số cuối cùng,

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
26, là một hàm gọi lại được gọi với một đối số lỗi có thể xảy ra. Nếu bất kỳ kiểm tra khả năng truy cập nào không thành công, đối số lỗi sẽ được điền. Ví dụ sau kiểm tra xem tệp
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
27 có thể được đọc và ghi bởi quy trình hiện tại không

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
0

Không nên sử dụng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
28 để kiểm tra khả năng truy cập tệp trước khi gọi
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29,
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30 hoặc
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
31. Làm như vậy sẽ đưa ra một điều kiện cạnh tranh, vì các quy trình khác có thể thay đổi trạng thái của tệp giữa hai lần gọi. Thay vào đó, mã người dùng nên trực tiếp mở/đọc/ghi tệp và xử lý lỗi phát sinh nếu tệp không thể truy cập được

Ví dụ

viết (KHÔNG ĐƯỢC KHUYẾN NGHỊ)

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
0

viết (KHUYẾN NGHỊ)

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
3

đọc (KHÔNG ĐƯỢC KHUYẾN NGHỊ)

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
0

đọc (KHUYẾN NGHỊ)

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
1

Các ví dụ "không được đề xuất" ở trên kiểm tra khả năng truy cập rồi sử dụng tệp;

Nói chung, chỉ kiểm tra khả năng truy cập của tệp nếu tệp không được sử dụng trực tiếp, chẳng hạn như khi khả năng truy cập tệp là tín hiệu từ một quy trình khác

fs. accessSync(đường dẫn[, chế độ])#

Phiên bản đồng bộ của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
28. Điều này ném nếu bất kỳ kiểm tra khả năng truy cập nào không thành công và không làm gì khác

fs. appendFile(file, data[, options], callback)#

Nối dữ liệu vào một tệp không đồng bộ, tạo tệp nếu nó chưa tồn tại.

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
33 có thể là một chuỗi hoặc một bộ đệm

Ví dụ

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
2

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một chuỗi, thì nó chỉ định mã hóa. Ví dụ

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
3

Mọi bộ mô tả tệp được chỉ định phải được mở để nối thêm

Ghi chú. Nếu một bộ mô tả tệp được chỉ định là

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
35, nó sẽ không tự động bị đóng

fs. appendFileSync(file, data[, options])#

Phiên bản đồng bộ của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
36. Trả lại
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. chmod(đường dẫn, chế độ, gọi lại)#

Thay đổi không đồng bộ các quyền của tệp. Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

Xem thêm. chmod(2)

fs. chmodSync(đường dẫn, chế độ)#

Đồng bộ thay đổi quyền của một tập tin. Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8. Đây là phiên bản đồng bộ của
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
39

Xem thêm. chmod(2)

fs. chown(đường dẫn, uid, gid, gọi lại)#

Thay đổi không đồng bộ chủ sở hữu và nhóm của tệp. Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

Xem thêm. chown(2)

fs. chownSync(đường dẫn, uid, gid)#

Đồng bộ thay đổi chủ sở hữu và nhóm của một tập tin. Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8. Đây là phiên bản đồng bộ của
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
41

Xem thêm. chown(2)

fs. đóng (fd, gọi lại) #

Đóng không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. closeSync(fd)#

Đóng đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. hằng số #

Trả về một đối tượng chứa các hằng số thường được sử dụng cho các hoạt động của hệ thống tệp. Các hằng số cụ thể hiện được xác định được mô tả trong Hằng số FS

fs. createReadStream(đường dẫn[, tùy chọn])#

Trả về một đối tượng

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
98 mới. (Xem Luồng có thể đọc)

Xin lưu ý rằng, không giống như giá trị mặc định được đặt cho

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
44 trên luồng có thể đọc được (16 kb), luồng được phương thức này trả về có giá trị mặc định là 64 kb cho cùng một tham số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một đối tượng hoặc chuỗi với các giá trị mặc định sau

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
4

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 có thể bao gồm các giá trị
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
47 và
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
48 để đọc một phạm vi byte từ tệp thay vì toàn bộ tệp. Cả
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
47 và
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
48 đều bao hàm và bắt đầu đếm từ 0. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99 được chỉ định và
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
47 bị bỏ qua hoặc
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8, thì
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
22 sẽ đọc tuần tự từ vị trí tệp hiện tại.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 có thể là bất kỳ một trong số đó được chấp nhận bởi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

Nếu

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99 được chỉ định, thì
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
98 sẽ bỏ qua đối số
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 và sẽ sử dụng bộ mô tả tệp được chỉ định. Điều này có nghĩa là sẽ không có sự kiện
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
60 nào được phát ra. Lưu ý rằng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99 sẽ bị chặn;

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
64 là sai, thì bộ mô tả tệp sẽ không bị đóng, ngay cả khi có lỗi. Bạn có trách nhiệm đóng nó lại và đảm bảo không có rò rỉ bộ mô tả tệp nào. Nếu
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
64 được đặt thành true (hành vi mặc định), trên
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
66 hoặc
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
48, bộ mô tả tệp sẽ tự động bị đóng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
13 đặt chế độ tệp (quyền và bit dính), nhưng chỉ khi tệp được tạo

Một ví dụ để đọc 10 byte cuối cùng của tệp dài 100 byte

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
5

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một chuỗi, thì nó chỉ định mã hóa

fs. createWriteStream(đường dẫn[, tùy chọn])#

Trả về một đối tượng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
01 mới. (Xem Luồng có thể ghi)

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một đối tượng hoặc chuỗi với các giá trị mặc định sau

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
6

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 cũng có thể bao gồm tùy chọn
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
47 để cho phép ghi dữ liệu ở một số vị trí sau phần đầu của tệp. Sửa đổi tệp thay vì thay thế nó có thể yêu cầu chế độ
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
74 của
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
75 thay vì chế độ mặc định
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
76.
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
77 có thể là bất kỳ một trong số đó được chấp nhận bởi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
64 được đặt thành true (hành vi mặc định) trên
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
66 hoặc
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
48, bộ mô tả tệp sẽ tự động bị đóng. Nếu
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
64 là sai, thì bộ mô tả tệp sẽ không bị đóng, ngay cả khi có lỗi. Bạn có trách nhiệm đóng nó lại và đảm bảo không có rò rỉ bộ mô tả tệp nào

Giống như

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
98, nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99 được chỉ định, thì
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
01 sẽ bỏ qua đối số
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 và sẽ sử dụng bộ mô tả tệp được chỉ định. Điều này có nghĩa là sẽ không có sự kiện
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
60 nào được phát ra. Lưu ý rằng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99 sẽ bị chặn;

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một chuỗi, thì nó chỉ định mã hóa

fs. tồn tại (đường dẫn, gọi lại) #

Đã thêm vào. v0. 0. 2 Không dùng nữa kể từ. v1. 0. 0

Kiểm tra xem đường dẫn đã cho có tồn tại hay không bằng cách kiểm tra với hệ thống tệp. Sau đó gọi đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
26 với giá trị true hoặc false. Ví dụ

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
7

Lưu ý rằng tham số cho cuộc gọi lại này không nhất quán với các Nút khác. gọi lại js. Thông thường, tham số đầu tiên cho một Nút. js gọi lại là một tham số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
93, tùy chọn theo sau là các tham số khác. Cuộc gọi lại
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94 chỉ có một tham số boolean. Đây là một lý do khiến
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
28 được đề xuất thay vì
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94

Không nên sử dụng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94 để kiểm tra sự tồn tại của tệp trước khi gọi
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29,
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30 hoặc
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
31. Làm như vậy sẽ đưa ra một điều kiện cạnh tranh, vì các quy trình khác có thể thay đổi trạng thái của tệp giữa hai lần gọi. Thay vào đó, mã người dùng nên mở/đọc/ghi tệp trực tiếp và xử lý lỗi phát sinh nếu tệp không tồn tại

Ví dụ

viết (KHÔNG ĐƯỢC KHUYẾN NGHỊ)

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
8

viết (KHUYẾN NGHỊ)

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
3

đọc (KHÔNG ĐƯỢC KHUYẾN NGHỊ)

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
0

đọc (KHUYẾN NGHỊ)

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
1

Các ví dụ "không được đề xuất" ở trên kiểm tra sự tồn tại và sau đó sử dụng tệp;

Nói chung, chỉ kiểm tra sự tồn tại của tệp nếu tệp không được sử dụng trực tiếp, chẳng hạn như khi sự tồn tại của tệp là tín hiệu từ một quy trình khác

fs. tồn tạiĐồng bộ hóa (đường dẫn) #

Phiên bản đồng bộ của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94. Trả về
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
02 nếu đường dẫn tồn tại, ngược lại là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
03

Lưu ý rằng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94 không được dùng nữa, nhưng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
05 thì không. (Tham số
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
26 đến
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
94 chấp nhận các tham số không phù hợp với các Node khác. gọi lại js.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
05 không sử dụng gọi lại. )

fs. fchmod(fd, chế độ, gọi lại)#

fchmod không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. fchmodSync(fd, chế độ)#

Fchmod đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. fchown(fd, uid, gid, gọi lại)#

fchown không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. fchown Sync(fire, uid, gid)#

Đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. fdatasync(fd, gọi lại)#

Fdatasync không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. fdatasyncSync(fd)#

Đồng bộ fdatasync(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. fstat(fd, gọi lại)#

fstat không đồng bộ(2). Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
12 trong đó
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
13 là một đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
15 giống với
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
16, ngoại trừ tệp được thống kê được chỉ định bởi bộ mô tả tệp
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99

fs. fstatSync(fd)#

fstat đồng bộ(2). Trả về một thể hiện của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14

fs. fsync(fd, gọi lại)#

fsync không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. fsyncSync(fd)#

Đồng bộ fsync(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. ftruncate(fd, len, gọi lại)

ftruncate không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

Nếu tệp được tham chiếu bởi bộ mô tả tệp lớn hơn

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
20 byte, thì chỉ
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
20 byte đầu tiên sẽ được giữ lại trong tệp

Ví dụ: chương trình sau chỉ giữ lại bốn byte đầu tiên của tệp

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
2

Nếu tệp trước đó ngắn hơn

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
20 byte, nó sẽ được mở rộng và phần mở rộng chứa đầy byte rỗng ('\0'). Ví dụ,

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
3

Ba byte cuối cùng là byte rỗng ('\0'), để bù cho việc cắt ngắn quá mức

fs. ftruncateSync(fd, len)

Rút ngắn đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. tương lai (fd, atime, mtime, gọi lại)

Thay đổi dấu thời gian tệp của tệp được tham chiếu bởi bộ mô tả tệp được cung cấp

Ghi chú. Chức năng này không hoạt động trên các phiên bản AIX trước 7. 1, nó sẽ trả về lỗi

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
24

fs. futimesSync(fd, atime, mtime)

Phiên bản đồng bộ của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
25. Trả lại
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. lchmod(đường dẫn, chế độ, gọi lại)#

Lchmod không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

Chỉ khả dụng trên macOS

fs. lchmodSync(đường dẫn, chế độ)#

Lchmod đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. lcown(đường dẫn, uid, gid, gọi lại)#

Lchown không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. lcownSync(đường dẫn, uid, gid)#

Lchown đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. liên kết (hiệnPath, newPath, gọi lại) #

Liên kết không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. linkSync( đường dẫn hiện tại, đường dẫn mới)

Liên kết đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. lstat(đường dẫn, gọi lại)#

Lstat không đồng bộ(2). Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
12 trong đó
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
13 là một đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
33 giống hệt với
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
16, ngoại trừ nếu
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 là một liên kết tượng trưng, ​​thì chính liên kết đó đã được thống kê, không phải tệp mà nó đề cập đến

fs. lstatSync(đường dẫn)#

Lstat đồng bộ(2). Trả về một thể hiện của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14

fs. mkdir(đường dẫn[, chế độ], gọi lại)#

Tạo thư mục không đồng bộ. Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành.

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
13 mặc định là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
38

Xem thêm. mkdir(2)

fs. mkdirSync(đường dẫn[, chế độ])#

Đồng bộ tạo một thư mục. Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8. Đây là phiên bản đồng bộ của
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
40

Xem thêm. mkdir(2)

fs. mkdtemp(tiền tố[, tùy chọn], gọi lại)#

Tạo một thư mục tạm thời duy nhất

Tạo sáu ký tự ngẫu nhiên được thêm vào sau một

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
41 bắt buộc để tạo một thư mục tạm thời duy nhất

Đường dẫn thư mục đã tạo được chuyển dưới dạng chuỗi tới tham số thứ hai của hàm gọi lại

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng

Ví dụ

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
4

Ghi chú. Phương thức

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
44 sẽ nối trực tiếp sáu ký tự được chọn ngẫu nhiên vào chuỗi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
41. Ví dụ: được cung cấp một thư mục
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
46, nếu mục đích là tạo một thư mục tạm thời trong
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
46, thì ____541 phải kết thúc bằng dấu phân cách đường dẫn dành riêng cho nền tảng (
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
49)

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
5

fs. mkdtempSync(tiền tố[, tùy chọn])#

Phiên bản đồng bộ của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
44. Trả về đường dẫn thư mục đã tạo

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng

fs. mở (đường dẫn, cờ [, chế độ], gọi lại) #

Mở tệp không đồng bộ. Xem mở(2).

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
74 có thể là

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    54 - Mở tệp để đọc. Một ngoại lệ xảy ra nếu tệp không tồn tại

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    55 - Mở tệp để đọc và ghi. Một ngoại lệ xảy ra nếu tệp không tồn tại

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    56 - Mở tệp để đọc và ghi ở chế độ đồng bộ. Hướng dẫn hệ điều hành bỏ qua bộ đệm ẩn của hệ thống tệp cục bộ

    Điều này chủ yếu hữu ích để mở các tệp trên các ngàm NFS vì nó cho phép bạn bỏ qua bộ đệm cục bộ có khả năng cũ. Nó có tác động rất lớn đến hiệu suất I/O, vì vậy đừng sử dụng cờ này trừ khi bạn cần

    Lưu ý rằng điều này không biến

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    29 thành cuộc gọi chặn đồng bộ. Nếu đó là điều bạn muốn thì bạn nên sử dụng
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    58

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    59 - Mở tệp để viết. Tệp được tạo (nếu không tồn tại) hoặc bị cắt bớt (nếu tồn tại)

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    60 - Giống như
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    59 nhưng không thành công nếu tồn tại
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    63 - Mở tệp để đọc và ghi. Tệp được tạo (nếu không tồn tại) hoặc bị cắt bớt (nếu tồn tại)

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    64 - Giống như
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    63 nhưng không thành công nếu tồn tại
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    67 - Mở tệp để nối thêm. Tệp được tạo nếu nó không tồn tại

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    68 - Giống như
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    67 nhưng không thành công nếu tồn tại
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    71 - Mở tệp để đọc và thêm vào. Tệp được tạo nếu nó không tồn tại

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    72 - Giống như
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    71 nhưng không thành công nếu tồn tại
    const fs = require('fs');
    
    fs.unlink('/tmp/hello', (err) => {
      if (err) throw err;
      console.log('successfully deleted /tmp/hello');
    });
    
    23

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
13 đặt chế độ tệp (quyền và bit dính), nhưng chỉ khi tệp được tạo. Nó mặc định là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
76 (có thể đọc và ghi)

Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
77

Cờ độc quyền

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
78 (cờ ______579 khi mở(2)) đảm bảo rằng
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 mới được tạo. Trên các hệ thống POSIX,
const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23 được coi là tồn tại ngay cả khi nó là một liên kết tượng trưng đến một tệp không tồn tại. Cờ độc quyền có thể hoặc không thể hoạt động với các hệ thống tệp mạng

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
74 cũng có thể là một số được ghi lại bởi open(2); . Trên Windows, các cờ được dịch sang các cờ tương đương nếu có, ví dụ:. g.
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
84 đến
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
85 hoặc
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
86 đến
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
87, như được chấp nhận bởi CreateFileW

Trên Linux, chức năng ghi theo vị trí không hoạt động khi tệp được mở ở chế độ chắp thêm. Hạt nhân bỏ qua đối số vị trí và luôn nối thêm dữ liệu vào cuối tệp

Ghi chú. Hành vi của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29 dành riêng cho nền tảng đối với một số cờ. Như vậy, việc mở một thư mục trên macOS và Linux bằng cờ
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
71 - xem ví dụ bên dưới - sẽ trả về lỗi. Ngược lại, trên Windows và FreeBSD, một bộ mô tả tệp sẽ được trả về

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
6

Một số ký tự (

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
90) được dành riêng trong Windows như được ghi lại bằng cách đặt tên tệp, đường dẫn và không gian tên. Trong NTFS, nếu tên tệp chứa dấu hai chấm, Nút. js sẽ mở một luồng hệ thống tệp, như được mô tả bởi this MSDN page

Các chức năng dựa trên

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29 cũng thể hiện hành vi này. ví dụ.
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
31,
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30, v.v.

fs. openSync(đường dẫn, cờ[, chế độ])#

Phiên bản đồng bộ của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29. Trả về một số nguyên đại diện cho bộ mô tả tệp

fs. read(fd, buffer, offset, length, position, callback)#

Đọc dữ liệu từ tệp được chỉ định bởi

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
99

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
96 là bộ đệm mà dữ liệu sẽ được ghi vào

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
97 là phần bù trong bộ đệm để bắt đầu ghi tại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
98 là một số nguyên chỉ định số byte cần đọc

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
99 là một đối số chỉ định bắt đầu đọc từ đâu trong tệp. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
99 là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
7, dữ liệu sẽ được đọc từ vị trí tệp hiện tại và vị trí tệp sẽ được cập nhật. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
99 là số nguyên, vị trí tệp sẽ không thay đổi

Cuộc gọi lại được đưa ra ba đối số,

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
03

fs. readdir(đường dẫn[, tùy chọn], gọi lại)#

Readdir không đồng bộ(3). Đọc nội dung của một thư mục. Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
04 trong đó
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
05 là một mảng tên của các tệp trong thư mục ngoại trừ
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
06 và
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
07

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho tên tệp được chuyển đến hàm gọi lại. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, tên tệp được trả về sẽ được chuyển thành đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. readdirSync(đường dẫn[, tùy chọn])#

Readdir đồng bộ(3). Trả về một mảng tên tệp không bao gồm

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
06 và
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
07

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho tên tệp được chuyển đến hàm gọi lại. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, tên tệp được trả về sẽ được chuyển thành đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. readFile(file[, options], callback)#

Đọc không đồng bộ toàn bộ nội dung của một tệp. Ví dụ

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
7

Cuộc gọi lại được thông qua hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
20, trong đó
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
33 là nội dung của tệp

Nếu không có mã hóa nào được chỉ định, thì bộ đệm thô được trả về

Nếu

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 là một chuỗi, thì nó chỉ định mã hóa. Ví dụ

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
8

Ghi chú. Khi đường dẫn là một thư mục, hành vi của

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30 và [
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
24][] là dành riêng cho nền tảng. Trên macOS, Linux và Windows, một lỗi sẽ được trả về. Trên FreeBSD, nội dung của thư mục sẽ được trả về

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
9

Bất kỳ bộ mô tả tệp được chỉ định nào cũng phải hỗ trợ đọc

Ghi chú. Nếu một bộ mô tả tệp được chỉ định là

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
23, nó sẽ không tự động bị đóng

fs. readFileSync(file[, options])#

Phiên bản đồng bộ của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
26. Trả về nội dung của
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
35

Nếu tùy chọn

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được chỉ định thì hàm này trả về một chuỗi. Nếu không, nó trả về một bộ đệm

Ghi chú. Tương tự như [

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30][], khi đường dẫn là một thư mục, hành vi của
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
24 là dành riêng cho nền tảng

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
0

fs. liên kết đọc(đường dẫn[, tùy chọn], gọi lại)#

Liên kết đọc không đồng bộ(2). Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
31

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho đường dẫn liên kết được chuyển đến lệnh gọi lại. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, đường dẫn liên kết được trả về sẽ được truyền dưới dạng đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. readlinkSync(đường dẫn[, tùy chọn])

Liên kết đọc đồng bộ(2). Trả về giá trị chuỗi của liên kết tượng trưng

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho đường dẫn liên kết được chuyển đến lệnh gọi lại. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, đường dẫn liên kết được trả về sẽ được truyền dưới dạng đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. readSync(fd, bộ đệm, độ lệch, độ dài, vị trí)#

Phiên bản đồng bộ của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
42. Trả về số của
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
43

fs. realpath(path[, options], callback)#

Đường dẫn thực không đồng bộ(3).

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
26 có hai đối số
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
45. Có thể sử dụng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
46 để giải quyết các đường dẫn tương đối

Chỉ hỗ trợ các đường dẫn có thể chuyển đổi thành chuỗi UTF8

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho đường dẫn được chuyển đến lệnh gọi lại. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, đường dẫn được trả về sẽ được truyền dưới dạng đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. realpathSync(đường dẫn[, tùy chọn])#

Đường dẫn thực đồng bộ(3). Trả về đường dẫn đã giải quyết

Chỉ hỗ trợ các đường dẫn có thể chuyển đổi thành chuỗi UTF8

Đối số

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
34 tùy chọn có thể là một chuỗi chỉ định mã hóa hoặc đối tượng có thuộc tính
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 chỉ định mã hóa ký tự sẽ sử dụng cho giá trị được trả về. Nếu
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
94 được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
95, đường dẫn được trả về sẽ được truyền dưới dạng đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92

fs. đổi tên (đường dẫn cũ, đường dẫn mới, gọi lại) #

Đổi tên không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. renameSync(oldPath, newPath)#

Đổi tên đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. rmdir(đường dẫn, gọi lại)#

rmdir không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

Ghi chú. Sử dụng

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
58 trên tệp (không phải thư mục) dẫn đến lỗi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
59 trên Windows và lỗi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
60 trên POSIX

fs. rmdirSync(đường dẫn)#

Đồng bộ rmdir(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

Ghi chú. Sử dụng

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
62 trên tệp (không phải thư mục) dẫn đến lỗi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
59 trên Windows và lỗi
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
60 trên POSIX

fs. stat(đường dẫn, gọi lại)#

Chỉ số không đồng bộ(2). Cuộc gọi lại nhận được hai đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
12 trong đó
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
13 là một đối tượng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14

Trong trường hợp xảy ra lỗi, thì

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
68 sẽ là một trong những Lỗi hệ thống thường gặp

Không nên sử dụng

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});
29 để kiểm tra sự tồn tại của tệp trước khi gọi
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
29,
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
30 hoặc
const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
31. Thay vào đó, mã người dùng nên trực tiếp mở/đọc/ghi tệp và xử lý lỗi phát sinh nếu tệp không khả dụng

Để kiểm tra xem một tệp có tồn tại mà không cần thao tác với nó sau đó hay không, nên sử dụng ____

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
28

fs. statSync(đường dẫn)#

Chỉ số đồng bộ(2). Trả về một thể hiện của

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
14

fs. liên kết tượng trưng (mục tiêu, đường dẫn [, loại], gọi lại) #

Liên kết tượng trưng không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành. Đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
75 có thể được đặt thành
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
76,
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
77 hoặc
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
78 (mặc định là
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
77) và chỉ khả dụng trên Windows (bỏ qua trên các nền tảng khác). Lưu ý rằng các điểm giao nhau của Windows yêu cầu đường dẫn đích phải tuyệt đối. Khi sử dụng
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
78, đối số
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
81 sẽ tự động được chuẩn hóa thành đường dẫn tuyệt đối

Dưới đây là một ví dụ dưới đây

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
1

Nó tạo ra một liên kết tượng trưng có tên là "new-port" trỏ đến "foo"

fs. symlinkSync(mục tiêu, đường dẫn[, loại])

Liên kết tượng trưng đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. cắt ngắn (đường dẫn, len, gọi lại)

Cắt ngắn không đồng bộ (2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành. Một bộ mô tả tệp cũng có thể được chuyển làm đối số đầu tiên. Trong trường hợp này,

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
83 được gọi là

fs. truncateSync(đường dẫn, len)

Cắt ngắn đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8. Một bộ mô tả tệp cũng có thể được chuyển làm đối số đầu tiên. Trong trường hợp này,
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
85 được gọi là

fs. hủy liên kết (đường dẫn, gọi lại) #

Hủy liên kết không đồng bộ(2). Không có đối số nào khác ngoài một ngoại lệ có thể được đưa ra cho cuộc gọi lại hoàn thành

fs. unlinkSync(đường dẫn)

Hủy liên kết đồng bộ(2). Trả lại

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. unwatchFile(tên tệp[, người nghe])#

Ngừng theo dõi các thay đổi trên

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90. Nếu
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
6 được chỉ định, chỉ người nghe cụ thể đó bị xóa. Nếu không, tất cả người nghe sẽ bị xóa và bạn đã ngừng xem
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90

Gọi

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90 với tên tệp không được xem là không hoạt động, không phải lỗi

Ghi chú.

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5 hiệu quả hơn
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92 và
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90.
$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
5 nên được sử dụng thay vì
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
92 và
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
90 khi có thể

fs. utimes(path, atime, mtime, callback)

Thay đổi dấu thời gian tệp của tệp được tham chiếu bởi đường dẫn được cung cấp

Ghi chú. các đối số

fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
01 và
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  console.log('renamed complete');
});
fs.stat('/tmp/world', (err, stats) => {
  if (err) throw err;
  console.log(`stats: ${JSON.stringify(stats)}`);
});
02 của các hàm liên quan sau tuân theo các quy tắc này

  • Giá trị phải là dấu thời gian Unix tính bằng giây. Ví dụ:
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99 trả về mili giây, vì vậy nó phải được chia cho 1000 trước khi chuyển vào
  • Nếu giá trị là một chuỗi số như
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    00, giá trị sẽ được chuyển đổi thành số tương ứng
  • Nếu giá trị là
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    01 hoặc
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    02, giá trị sẽ được chuyển đổi thành
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    03

fs. utimesSync(đường dẫn, atime, mtime)

Phiên bản đồng bộ của

$ cat script.js
function bad() {
  require('fs').readFile('/');
}
bad();

$ env NODE_DEBUG=fs node script.js
fs.js:88
        throw backtrace;
        ^
Error: EISDIR: illegal operation on a directory, read
    
04. Trả lại
fs.rename('/tmp/hello', '/tmp/world', (err) => {
  if (err) throw err;
  fs.stat('/tmp/world', (err, stats) => {
    if (err) throw err;
    console.log(`stats: ${JSON.stringify(stats)}`);
  });
});
8

fs. đồng hồ (tên tệp [, tùy chọn] [, người nghe]) #

  • fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90.
  • const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34.
    • $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      08 Cho biết liệu quy trình có nên tiếp tục chạy miễn là các tệp đang được xem hay không. mặc định =
      fs.rename('/tmp/hello', '/tmp/world', (err) => {
        if (err) throw err;
        console.log('renamed complete');
      });
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
      
      02
    • $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      10 Cho biết nên xem tất cả các thư mục con hay chỉ thư mục hiện tại. Điều này áp dụng khi một thư mục được chỉ định và chỉ trên các nền tảng được hỗ trợ (Xem Lưu ý). mặc định =
      fs.rename('/tmp/hello', '/tmp/world', (err) => {
        if (err) throw err;
        console.log('renamed complete');
      });
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
      
      03
    • fs.rename('/tmp/hello', '/tmp/world', (err) => {
        if (err) throw err;
        fs.stat('/tmp/world', (err, stats) => {
          if (err) throw err;
          console.log(`stats: ${JSON.stringify(stats)}`);
        });
      });
      
      94 Chỉ định mã hóa ký tự được sử dụng cho tên tệp được truyền cho người nghe. mặc định =
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      13
  • $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    6
  • Theo dõi các thay đổi trên

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90, trong đó
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90 là tệp hoặc thư mục. Đối tượng được trả về là một
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    97

    Đối số thứ hai là tùy chọn. Nếu

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 được cung cấp dưới dạng một chuỗi, thì nó chỉ định
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    94. Mặt khác,
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 phải được chuyển thành một đối tượng

    Cuộc gọi lại của người nghe nhận được hai đối số

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    21.
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    22 là
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    23 hoặc
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    24 và
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90 là tên của tệp đã kích hoạt sự kiện

    Lưu ý rằng trên hầu hết các nền tảng,

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    23 được phát ra bất cứ khi nào tên tệp xuất hiện hoặc biến mất trong thư mục

    Cũng lưu ý rằng cuộc gọi lại của người nghe được đính kèm với sự kiện

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    24 được kích hoạt bởi
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    97, nhưng nó không giống với giá trị
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    24 của
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    22

    Hãy cẩn thận #

    API

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    31 không nhất quán 100% trên các nền tảng và không khả dụng trong một số trường hợp

    Tùy chọn đệ quy chỉ được hỗ trợ trên macOS và Windows

    Khả dụng#

    Tính năng này phụ thuộc vào hệ điều hành cơ bản cung cấp cách thức được thông báo về các thay đổi của hệ thống tệp

    • Trên các hệ thống Linux, điều này sử dụng
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      32
    • Trên các hệ thống BSD, điều này sử dụng
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      33
    • Trên macOS, điều này sử dụng
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      33 cho tệp và
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      35 cho thư mục
    • Trên các hệ thống SunOS (bao gồm cả Solaris và SmartOS), điều này sử dụng
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      36
    • Trên hệ thống Windows, tính năng này phụ thuộc vào
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      37
    • Trên các hệ thống Aix, tính năng này phụ thuộc vào
      $ cat script.js
      function bad() {
        require('fs').readFile('/');
      }
      bad();
      
      $ env NODE_DEBUG=fs node script.js
      fs.js:88
              throw backtrace;
              ^
      Error: EISDIR: illegal operation on a directory, read
          
      
      38, tính năng này phải được bật

    Nếu chức năng cơ bản không khả dụng vì lý do nào đó, thì

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    31 sẽ không thể hoạt động. Ví dụ: việc xem tệp hoặc thư mục có thể không đáng tin cậy và trong một số trường hợp là không thể trên hệ thống tệp mạng (NFS, SMB, v.v.) hoặc hệ thống tệp máy chủ khi sử dụng phần mềm ảo hóa như Vagrant, Docker, v.v.

    Bạn vẫn có thể sử dụng

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    40, sử dụng tính năng bỏ phiếu theo thống kê, nhưng chậm hơn và kém tin cậy hơn

    nút #

    Trên các hệ thống Linux và macOS,

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    5 giải quyết đường dẫn đến một inode và xem inode đó. Nếu đường dẫn đã xem bị xóa và tạo lại, nó sẽ được gán một nút mới. Đồng hồ sẽ phát ra một sự kiện để xóa nhưng sẽ tiếp tục xem inode ban đầu. Các sự kiện cho inode mới sẽ không được phát ra. Đây là hành vi dự kiến

    Trên AIX, lưu và đóng tệp đang được xem sẽ gây ra hai thông báo - một để thêm nội dung mới và một để cắt bớt. Ngoài ra, các thao tác lưu và đóng trên một số nền tảng gây ra các thay đổi inode buộc các thao tác trên đồng hồ trở nên không hợp lệ và không hiệu quả. AIX giữ lại inode trong suốt thời gian tồn tại của tệp, theo cách đó, mặc dù điều này khác với Linux/OS X, điều này giúp cải thiện khả năng sử dụng của việc xem tệp. Đây là hành vi dự kiến

    Đối số tên tệp #

    Việc cung cấp đối số

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90 trong hàm gọi lại chỉ được hỗ trợ trên Linux và Windows. Ngay cả trên các nền tảng được hỗ trợ,
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90 không phải lúc nào cũng được đảm bảo cung cấp. Do đó, đừng cho rằng đối số
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90 luôn được cung cấp trong hàm gọi lại và có một số logic dự phòng nếu nó là null

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    2

    fs. watchFile(tên tệp[, tùy chọn], người nghe)#

    Theo dõi những thay đổi trên

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    90. Cuộc gọi lại
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    6 sẽ được gọi mỗi khi tệp được truy cập

    Đối số

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 có thể được bỏ qua. Nếu được cung cấp, nó phải là một đối tượng. Đối tượng
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 có thể chứa một giá trị logic có tên là
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    08 cho biết liệu quy trình có nên tiếp tục chạy hay không khi các tệp đang được xem. Đối tượng
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 có thể chỉ định thuộc tính
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    51 cho biết tần suất mục tiêu sẽ được thăm dò tính bằng mili giây. Mặc định là
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    52

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    6 nhận hai đối số đối tượng stat hiện tại và đối tượng stat trước đó

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    3

    Các đối tượng thống kê này là các thể hiện của

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    54

    Nếu bạn muốn được thông báo khi tệp được sửa đổi, không chỉ được truy cập, bạn cần so sánh

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    55 và
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    56

    Ghi chú. khi một hoạt động của

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    40 dẫn đến lỗi
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    59, nó sẽ gọi trình nghe một lần, với tất cả các trường bằng 0 (hoặc, đối với ngày, Unix Epoch). Trong Windows, các trường
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    59 và
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    60 sẽ là
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    8, thay vì 0. Nếu tệp được tạo sau này, trình nghe sẽ được gọi lại, với các đối tượng thống kê mới nhất. Đây là một thay đổi về chức năng kể từ v0. 10

    Ghi chú.

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    5 hiệu quả hơn
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    40 và
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    64.
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    31 nên được sử dụng thay vì
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    40 và
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    64 khi có thể

    Ghi chú. Khi một tệp đang được xem bởi

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    92 biến mất và xuất hiện lại, thì
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    69 được báo cáo trong sự kiện gọi lại thứ hai (tệp xuất hiện trở lại) sẽ giống như
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    69 của sự kiện gọi lại đầu tiên (sự biến mất của nó)

    Điều này xảy ra khi

    • tệp bị xóa, sau đó là khôi phục
    • tệp được đổi tên hai lần - lần thứ hai trở lại tên ban đầu

    fs. write(fd, buffer[, offset[, length[, position]]], callback)#

    Viết

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    96 vào tệp được chỉ định bởi
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    97 xác định phần bộ đệm sẽ được ghi và
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    98 là số nguyên chỉ định số byte cần ghi

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    99 đề cập đến phần bù từ đầu tệp nơi dữ liệu này sẽ được ghi. Nếu
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    76, dữ liệu sẽ được ghi ở vị trí hiện tại. Xem pwrite(2)

    Cuộc gọi lại sẽ được cung cấp ba đối số

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    77 trong đó
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    78 chỉ định có bao nhiêu byte được viết từ
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    96

    Lưu ý rằng việc sử dụng

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    80 nhiều lần trên cùng một tệp mà không chờ gọi lại là không an toàn. Đối với kịch bản này, chúng tôi khuyên bạn nên sử dụng
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    81

    Trên Linux, chức năng ghi theo vị trí không hoạt động khi tệp được mở ở chế độ chắp thêm. Hạt nhân bỏ qua đối số vị trí và luôn nối thêm dữ liệu vào cuối tệp

    fs. write(fd, chuỗi[, vị trí[, mã hóa]], gọi lại)#

    Viết

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    82 vào tệp được chỉ định bởi
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    99. Nếu
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    82 không phải là một chuỗi, thì giá trị sẽ bị ép thành một

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    99 đề cập đến phần bù từ đầu tệp nơi dữ liệu này sẽ được ghi. Nếu
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    76 dữ liệu sẽ được ghi ở vị trí hiện tại. Xem pwrite(2)

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    94 là mã hóa chuỗi dự kiến

    Cuộc gọi lại sẽ nhận các đối số

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    88 trong đó
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    78 chỉ định số lượng byte mà chuỗi đã truyền cần được ghi. Lưu ý rằng các byte được viết không giống như các ký tự chuỗi. Xem
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    90

    Khác với khi viết

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    96 thì phải viết cả chuỗi. Không có chuỗi con có thể được chỉ định. Điều này là do độ lệch byte của dữ liệu kết quả có thể không giống với độ lệch chuỗi

    Lưu ý rằng việc sử dụng

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    80 nhiều lần trên cùng một tệp mà không chờ gọi lại là không an toàn. Đối với kịch bản này, chúng tôi khuyên bạn nên sử dụng
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    81

    Trên Linux, chức năng ghi theo vị trí không hoạt động khi tệp được mở ở chế độ chắp thêm. Hạt nhân bỏ qua đối số vị trí và luôn nối thêm dữ liệu vào cuối tệp

    fs. writeFile(file, data[, options], callback)#

    Ghi dữ liệu vào tệp không đồng bộ, thay thế tệp nếu tệp đã tồn tại.

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    33 có thể là một chuỗi hoặc một bộ đệm

    Tùy chọn

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    94 bị bỏ qua nếu
    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    33 là bộ đệm. Nó mặc định là
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    13

    Ví dụ

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    4

    Nếu

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    34 là một chuỗi, thì nó chỉ định mã hóa. Ví dụ

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    5

    Bất kỳ bộ mô tả tệp được chỉ định nào cũng phải hỗ trợ ghi

    Lưu ý rằng việc sử dụng

    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    99 nhiều lần trên cùng một tệp mà không chờ gọi lại là không an toàn. Đối với kịch bản này, chúng tôi khuyên bạn nên sử dụng
    $ cat script.js
    function bad() {
      require('fs').readFile('/');
    }
    bad();
    
    $ env NODE_DEBUG=fs node script.js
    fs.js:88
            throw backtrace;
            ^
    Error: EISDIR: illegal operation on a directory, read
        
    
    81

    Ghi chú. Nếu một bộ mô tả tệp được chỉ định là

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    35, nó sẽ không tự động bị đóng

    fs. writeFileSync(file, data[, options])#

    Phiên bản đồng bộ của

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    31. Trả lại
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    8

    fs. writeSync(fd, buffer[, offset[, length[, position]]])#

    fs. writeSync(fd, chuỗi[, vị trí[, mã hóa]])#

    Các phiên bản đồng bộ của

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      fs.stat('/tmp/world', (err, stats) => {
        if (err) throw err;
        console.log(`stats: ${JSON.stringify(stats)}`);
      });
    });
    
    904. Trả về số byte đã ghi

    Hằng số FS#

    Các hằng số sau đây được xuất bởi

    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    83. Ghi chú. Không phải mọi hằng số sẽ có sẵn trên mọi hệ điều hành

    Các hằng số truy cập tệp #

    Các hằng số sau đây được dùng với thuộc tính

    const fs = require('fs');
    
    fs.unlinkSync('/tmp/hello');
    console.log('successfully deleted /tmp/hello');
    
    13 của đối tượng
    fs.rename('/tmp/hello', '/tmp/world', (err) => {
      if (err) throw err;
      console.log('renamed complete');
    });
    fs.stat('/tmp/world', (err, stats) => {
      if (err) throw err;
      console.log(`stats: ${JSON.stringify(stats)}`);
    });
    
    14 để xác định quyền truy cập đối với một tệp