1var fs = require('fs');
2
3// Save the string "Hello world!" in a file called "hello.txt" in
4// the directory "/tmp" using the default encoding (utf8).
5// This operation will be completed in background and the callback
6// will be called when it is either done or failed.
7fs.writeFile('/tmp/hello.txt', 'Hello world!', function(err) {
8 // If an error occurred, show it and return
9 if(err) return console.error(err);
10 // Successfully wrote to the file!
11});
12
13// Save binary data to a file called "binary.txt" in the current
14// directory. Again, the operation will be completed in background.
15var buffer = new Buffer([ 0x48, 0x65, 0x6c, 0x6c, 0x6f ]);
16fs.writeFile('binary.txt', buffer, function(err) {
17 // If an error occurred, show it and return
18 if(err) return console.error(err);
19 // Successfully wrote binary contents to the file!
20});
21