Efficient Methods for Reading Local Text Files into JavaScript Arrays

Nov 25, 2025 · Programming · 7 views · 7.8

Keywords: JavaScript | File Reading | Node.js | Array Processing | Text Parsing

Abstract: This article comprehensively explores various approaches to read local text files and convert their contents into arrays in JavaScript environments. It focuses on synchronous and asynchronous file reading using Node.js file system module, including key technical details like Buffer conversion and encoding handling. The article also compares alternative solutions in browser environments, such as user interaction or preloaded scripts. Through complete code examples and performance analysis, it helps developers choose optimal solutions based on specific scenarios.

Fundamental Principles of File Reading

In modern web development, processing local files is a common requirement. JavaScript, as the dominant frontend language, provides multiple file processing mechanisms. Implementation approaches vary significantly depending on the runtime environment.

File Reading in Node.js Environment

Node.js provides comprehensive file system operations through its built-in fs module. For text file reading, it primarily supports synchronous and asynchronous modes.

Synchronous Reading Mode

Synchronous reading blocks code execution until file reading completes. This approach offers simple code structure and is suitable for scenarios like loading configuration files during application startup.

var fs = require("fs");
var text = fs.readFileSync("./mytext.txt").toString('utf-8');
var textByLine = text.split("\n");
console.log(textByLine); // Output: ["red", "green", "blue", "black"]

In Node.js version 6 and above, readFileSync returns a Buffer object by default, requiring conversion to string via toString() method or encoding parameter specification. Two equivalent implementation approaches:

// Approach 1: Convert after reading
var text = fs.readFileSync("./mytext.txt").toString('utf-8');

// Approach 2: Specify encoding during reading
var text = fs.readFileSync("./mytext.txt", "utf-8");

Asynchronous Reading Mode

Asynchronous reading doesn't block the event loop, making it suitable for handling large files or maintaining application responsiveness.

var fs = require("fs");
fs.readFile("./mytext.txt", "utf-8", function(err, text) {
    if (err) throw err;
    var textByLine = text.split("\n");
    console.log(textByLine);
});

Alternative Solutions in Browser Environment

Due to security restrictions, JavaScript in browsers cannot directly access the local file system. However, similar functionality can be achieved through the following approaches:

User Interaction Approach

Allow users to select files through file input elements:

<input type="file" id="fileInput" />
<script>
document.getElementById('fileInput').addEventListener('change', function(e) {
    var file = e.target.files[0];
    var reader = new FileReader();
    reader.onload = function(e) {
        var text = e.target.result;
        var textByLine = text.split("\n");
        console.log(textByLine);
    };
    reader.readAsText(file);
});
</script>

Preloaded Script Solution

Store data in separate JavaScript files beforehand:

// data.js
var visibleItems = [
    "City Limits",
    "Land",
    "Parcels",
    "Boundary"
];

Include and use directly in HTML:

<script src="data.js"></script>
<script>
for(var i = 0; i < myItems.length; i++) {
    var item = myItems[i];
    if(visibleItems.indexOf(item.title) !== -1) {
        item.visible = true;
    }
}
</script>

Performance Optimization and Best Practices

When handling large text files, consider memory usage and performance aspects:

Stream Processing for Large Files

For files exceeding memory limits, use stream-based reading:

var fs = require("fs");
var readline = require("readline");
var array = [];

var rl = readline.createInterface({
    input: fs.createReadStream("./largefile.txt"),
    output: process.stdout,
    terminal: false
});

rl.on('line', function(line) {
    array.push(line);
});

rl.on('close', function() {
    console.log('File reading completed', array.length);
});

Error Handling Mechanisms

Robust file reading requires comprehensive error handling:

try {
    var text = fs.readFileSync("./mytext.txt", "utf-8");
    var textByLine = text.split("\n").filter(function(line) {
        return line.trim() !== ""; // Filter empty lines
    });
} catch (error) {
    console.error("File reading failed:", error.message);
}

Application Scenario Analysis

Different application scenarios suit different implementation approaches:

By appropriately selecting implementation methods, developers can ensure functional completeness while optimizing application performance and user experience.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.