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

On my way to the 3D city model workshop in Bonn where I‘m presenting our latest @igeoFHNW #AugmentedReality App… https://t.co/8ghq4GfZW5
About 5 days, 6 hours ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

Teaching #Python to 11-13-year-old girls was great fun today at the Swiss national future day (#zukunftstag).… https://t.co/1lDmWdl02D
About 1 week, 2 days ago by: Martin Christen (@MartinChristen)

MartinChristen avatar

RT @GeoPythonConf: Interested in joining GeoPython 2019? Register to the mailing list: https://t.co/ub3CU7L5WL #python #GeoPython #geospat…
About 2 weeks, 2 days 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