Hướng dẫn javascript write text file w3schools - javascript viết tệp văn bản w3schools


Node.js như một máy chủ tệp

Mô -đun hệ thống tệp Node.js cho phép bạn làm việc với hệ thống tệp trên máy tính của bạn.

Để bao gồm mô -đun hệ thống tệp, hãy sử dụng phương thức require():

Sử dụng phổ biến cho mô -đun hệ thống tệp:

  • Đọc tệp
  • Tạo tập tin
  • Cập nhật tệp
  • Xóa các tập tin
  • Đổi tên tập tin

Đọc tệp

Phương thức fs.readFile() được sử dụng để đọc các tệp trên máy tính của bạn.

Giả sử chúng ta có tệp HTML sau (nằm trong cùng thư mục với Node.js):

demofile1.html

Đoạn đầu của tôi.

My Header


My paragraph.



Tạo tệp Node.js đọc tệp HTML và trả về nội dung:

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Bắt đầu demo_readfile.js:

C: \ user \ tên của bạn> node demo_readfile.js

Nếu bạn đã làm theo cùng các bước trên máy tính của mình, bạn sẽ thấy kết quả tương tự như ví dụ: http: // localhost: 8080



Tạo tập tin

Mô -đun hệ thống tệp có các phương thức tạo tệp mới:

  • fs.appendFile()
  • fs.open()
  • fs.writeFile()

Phương thức fs.appendFile() nối nội dung được chỉ định vào một tệp. Nếu tệp không tồn tại, tệp sẽ được tạo:

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('Saved!');
});

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('Saved!');
});

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('Saved!');
});

Bắt đầu demo_readfile.js:


C: \ user \ tên của bạn> node demo_readfile.js

Nếu bạn đã làm theo cùng các bước trên máy tính của mình, bạn sẽ thấy kết quả tương tự như ví dụ: http: // localhost: 8080

  • fs.appendFile()
  • fs.writeFile()

Tạo tập tin

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('Updated!');
});

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('Replaced!');
});

Chạy ví dụ »


Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Bắt đầu demo_readfile.js:

C: \ user \ tên của bạn> node demo_readfile.js

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('File deleted!');
});

Chạy ví dụ »


Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Bắt đầu demo_readfile.js:

C: \ user \ tên của bạn> node demo_readfile.js

Thí dụ

var http = yêu cầu ('http'); var fs = yêu cầu ('fs'); http.createserver (function (req, res) {& nbsp; fs.readfile ('demofile1.html', function (err, data) & NBSP; (); & nbsp;});}). Nghe (8080);

Chạy ví dụ »

Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
  if (err) throw err;
  console.log('File Renamed!');
});

Chạy ví dụ »


Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:

Bắt đầu demo_readfile.js:

C: \ user \ tên của bạn> node demo_readfile.js



Bạn có thể viết vào một tệp văn bản trong JavaScript không?

Có một mô-đun tích hợp hoặc thư viện được xây dựng trong NodeJS, xử lý tất cả các hoạt động viết được gọi là FS (hệ thống tệp).Về cơ bản, nó là một chương trình JavaScript (fs. JS) trong đó chức năng cho các hoạt động viết được viết.Nhập mô hình FS trong chương trình và sử dụng các chức năng để ghi văn bản vào các tệp trong hệ thống.. It is basically a JavaScript program (fs. js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system.

Làm thế nào để bạn viết văn bản trong HTML bằng JavaScript?

Đầu ra JavaScript..
Viết vào một phần tử HTML, sử dụng InternalHTML ..
Viết vào đầu ra HTML bằng document.write () ..
Viết vào một hộp cảnh báo, sử dụng window.alert () ..
Viết vào bảng điều khiển trình duyệt, sử dụng Console.log () ..

Làm thế nào để bạn viết trong JavaScript?

Để viết JavaScript, bạn cần trình duyệt web và trình chỉnh sửa văn bản hoặc trình soạn thảo HTML.Khi bạn có phần mềm tại chỗ, bạn có thể bắt đầu viết mã JavaScript.Để thêm mã JavaScript vào tệp HTML, hãy tạo hoặc mở tệp HTML với trình soạn thảo văn bản/HTML của bạn.you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.