- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Load a text file and find number of characters in the file - JavaScript
Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is −
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s.
We are required to write a JavaScript function that loads this external text file into our js file and returns the number of characters in this file.
Example
Let’s write the code for this function −
const fs = require('fs'); const requireFile = async () => { const data = fs.readFileSync('./data.txt', 'utf-8'); const len = data.length; return len; }; requireFile().then(res => console.log(res)).catch(err => { console.log('some error occured'); })
Output
The output in the console: −
399
- Related Articles
- Counting number of characters in text file using java
- How to read a number of characters from a text file using Python?
- How to count the number of characters (including spaces) in a text file using Java?
- How to load a JavaScript file Dynamically?
- How to Remove Newline Characters from a text File?
- C program to count characters, lines and number of words in a file
- How to Create and Save text file in JavaScript?
- How to Find Line Number of a Given Word in text file using Python?
- Counting number of paragraphs in text file using java
- Counting number of lines in text file using java
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- How to find and replace the word in a text file using PowerShell?
- How to find the Strings within a text file in Java?
- How to find and replace within a text file using Python?

Advertisements