User Tools

Site Tools


Sidebar

General

Python Tutorials

WebGL Tutorials

Node.js Tutorials

Node.js in WebStorm


Martin Christen

View Martin Christen's profile on LinkedIn


Twitter

Timeline of @MartinChristen

MartinChristen avatar

I just signed up as @europython volunteer. Still many spaces available! Do it now! https://t.co/Bam6ZJCjFR
About 1 day, 23 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

@Mr_Robinini I will do a talk about this at @EuroSciPy in August...
About 2 days, 22 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

@Mr_Robinini Try jupyterhub, you can start jupyterlab too (change config file). https://t.co/kDKr9oV946
About 2 days, 22 hours ago by: Martin Christen (@MartinChristen)
node:tutorial05

Node.js Tutorials - Part 0

Node.js Tutorial 5

Reading and writing files is something you can't do in standard JavaScript. In node.js file handling comes with the built in file system module called “fs”. The module is loaded using:

var fs = require('fs');

Writing Files

Writing a whole file can be done with the “writeFile” function. This requires a buffer containing the whole contents of the file. There are two versions of this function: The first version “writeFile” is asynchronous and the second version “writeFileSync” is synchronous.

var data = "Hello World, this is a text...";
var filename = "file.txt";

fs.writeFile(filename, data , function (err) {
  if (err) throw err;
  console.log('Ok. File written...');
});

Reading Files

Reading a whole file can be done with the “readFile” function. There is also a synchronous and an asynchronous version of this function.

var filename = "file.txt";

fs.readFile(filename, function (err, data) {
  if (err) throw err;
  console.log("" + data);
});


node/tutorial05.txt · Last modified: 2013/09/16 22:05 by mchristen