jsrosetta

I/O

Reading, Writing, and Deleting Files

How Node.js's readFile/writeFile compares to os.ReadFile/os.WriteFile (Go), std::fs (Rust), FileManager (Swift), and java.nio.file.Files (Java).

Minimum versions
Node.js ≥ 14.13.1Go ≥ 1.16Rust ≥ 1.58Swift ≥ 3.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

All five languages have high-level APIs for creating, reading, and deleting files without managing file descriptors by hand. Node.js uses Promise-based functions in node:fs/promises; Go returns (value, error) directly — no exceptions, just an err you check right after each call; Rust also returns a Result, but uses ? to propagate the error instead of checking it manually; Swift and Java use exceptions (throws/try) like JavaScript, just stricter about error types.

Creating, writing, reading, and deleting a file

import { readFile, unlink, writeFile } from 'node:fs/promises'
 
// create file (and write to it)
await writeFile('test.txt', 'hello world.')
 
// read file
const contents = await readFile('test.txt', 'utf8')
console.log(contents)
 
// delete file
await unlink('test.txt')
hello world.

Reference: github.com/miguelmota/golang-for-nodejs-developers#files