Welcome to the first lesson in our course on "Fundamentals of Text Data Manipulation." This lesson will introduce you to the essential skill of reading text files using JavaScript, specifically with Node.js. Text files are a vital data source in programming, commonly used for storing data, configuration files, and logs. Being able to open and read files in JavaScript is a foundational skill you'll often rely on when working with data. By the end of this lesson, you will be able to read the entire contents of a text file into a string, an essential skill for various data manipulation tasks. Let's get started!
A file path is essentially the address of a file in your system's storage. It tells your program where to find or save a file. There are two types of file paths:
-
Absolute Path: This is the full path to a file, starting from the root directory. Here are some examples from different operating systems:
- Linux:
/home/user/documents/input.txt
- Mac:
/Users/user/documents/input.txt
- Windows:
C:\\Users\\user\\documents\\input.txt
- Linux:
-
Relative Path: This path is relative to the directory from which the script is executed. For example,
documents/input.txt
assumes your script is running from theuser
directory in the examples above.
Here's how you can specify a file path in JavaScript using Node.js:
JavaScript1const filePath = 'input.txt'; // Relative path
Make sure your Node.js script and the text file are in the same directory if you use a relative path. Otherwise, use the absolute path to ensure that Node.js can find your file.
When working with relative paths, it's important to understand the structure of your directories. Here are a few examples with file trees:
-
Example 1:
File Tree:
Plain text1project/ 2├── script.js 3└── data/ 4 └── input.txt
Relative Path:
JavaScript1const filePath = 'data/input.txt';
-
Example 2:
File Tree:
Plain text1user/ 2├── documents/ 3│ └── script.js 4└── input.txt
Relative Path:
JavaScript1const filePath = '../input.txt';
The
..
is used to navigate to the parent directory. It works this way in both MacOS/Linux and Windows. -
Example 3:
File Tree:
Plain text1application/ 2├── scripts/ 3│ ├── script1.js 4│ └── script2.js 5└── resources/ 6 └── input.txt
Relative Path (assuming the script is in
script2.js
):JavaScript1const filePath = '../resources/input.txt';
These examples illustrate how relative paths depend on the current working directory of your script.
Note: When using relative paths, Node.js starts from the directory where the script was executed. If your script is run from a different directory (e.g., via a terminal command), it might fail to find the file. To avoid such issues, you can use __dirname
to dynamically get the current directory of the script.
JavaScript1const path = require('path'); 2const filePath = path.join(__dirname, 'input.txt');
The path.join()
method joins the specified path segments into one path. You can specify as many path segments as you like.
In JavaScript with Node.js, the fs.readFileSync()
function is used to open and read the contents of a file in one step. This method requires several arguments:
- Path: The path to the file you want to read.
- Options/Encoding: An optional argument where you can specify the character encoding. The most common encoding for text files is
'utf-8'
.
Here's an example:
JavaScript1const fs = require('fs'); 2const filePath = 'input.txt'; 3 4const content = fs.readFileSync(filePath, 'utf-8'); // Reads the file at the given path with 'utf-8' encoding 5console.log("Full file content:"); 6console.log(content);
In this code snippet, content
stores the entire contents of the file as a string, which you can then process or display.
In this lesson, you've learned how to:
- Specify file paths correctly with examples from different operating systems.
- Use Node.js's
fs
module to read the contents of a file with thefs.readFileSync()
function. - Define relative paths correctly with examples and file tree illustrations.
These foundational skills will serve you well in handling data stored in text files. As you move on to the practice exercises, you'll apply these concepts by reading different text files and extracting their content. This hands-on experience will solidify your understanding, preparing you for more advanced file manipulation techniques in the future. Keep up the good work, and happy coding!