29

I am attempting to convert a text file (on my machine) into a string. What is the best/simplest way to do this? I am looking for a basic function that would look like this:

function fileToString(filepath) {
   //this returns a string with the contents of the file
}

How should I do this?

EDIT: I now know that there is another question that asks this, but I didn't understand that question so I asked it in different words.

3

3 Answers 3

41

You need node.js for that and this code:

const fs = require('fs')

const fileContents = fs.readFileSync('./myFile').toString()
Sign up to request clarification or add additional context in comments.

2 Comments

fs.readFileSync('./myFile', 'utf8') returns a string. No need the .toString() nodejs.org/api/fs.html#fs_fs_readfilesync_path_options
Exactly! That's why it's a better practice to use the function as it is designed adding 'utf8' as encoding option. Otherwise, it returns a buffer that you have to convert into a string. Why would you ask the function to return a buffer when it can return the type you want?
12

You need to use Node.js for that. The code would be:

const fs = require('fs');
const fileName = "myFile.txt";
const fileData = fs.readFileSync(fileName, "utf8");

4 Comments

This answer is incorrect. readFile does not return file contents. On the other hand readFileSync does (but not a string but a buffer).
"If the encoding option is specified then this function returns a string. Otherwise it returns a buffer." from the nodejs docs nodejs.org/api/fs.html#fs_fs_readfilesync_path_options. Sheesh.
I believe I have fixed it now @Mark - but the docs were a little unclear. Should it be string or utf-8?
(filename, "utf8")
1

You cannot do this in Javascript (browser based), as it does not have access to the file system. You have to use NodeJs for the same.

var fs = require('fs');

fs.readFile('DATA', 'utf8', function(err, contents) {
    console.log(contents);
});

This willl print the contents of the file. Store the contents in a variable. JS has a .toString() function which can do what you want.

8 Comments

Saying that "You cannot do this in Javascript" is incorrect and can be misleading for beginners. I think it's better to say that the browser-based JS runtime systems do not support FileSystem access. Node.js, another JS runtime system (not browser-based), supports that.
Funny thing that it is possible to read files with locally running JavaScript for almost 20 years now on Windows...
That's true, good point.
@NurbolAlpysbayev No, NodeJS is not the first command line runner for JavaScript available on Windows - CScript/WScript where there before 2000, .Net compiler (jsc) is there since about 2001 (en.wikipedia.org/wiki/JScript_.NET). You can find samples stackoverflow.com/questions/28881184/… or if you need more info MSDN contains plenty as well as SO. Search - bing.com/search?q=cscript%20command%20javascript
@NurbolAlpysbayev there is also HTA that let you run HTML pages with script as local apps with mostly full access to local resources (since 1999).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.