Welcome to this lesson, where we'll explore an essential technique in text data manipulation: reading files line-by-line with TypeScript. In many real-world applications, processing data one line at a time is crucial for effective data management, especially when handling large files like logs or data streams. By the end of this lesson, you'll understand how to efficiently read and process file data line-by-line, leveraging TypeScript's capabilities with Node.js.
In TypeScript, when using Node.js, we handle file operations with the fs
module. To open a file and read its contents, we'll utilize fs.readFileSync()
. However, TypeScript adds the advantage of type safety. Here's an example:
TypeScript1import * as fs from 'fs'; 2 3const filePath: string = 'input.txt'; 4const content: string = fs.readFileSync(filePath, 'utf-8');
In this example, filePath
is explicitly declared as a string, indicating the location of your file, and content
is a string containing the entire file's content, read synchronously.
Reading a file line-by-line in TypeScript is straightforward. We'll use the split()
method, but with explicit type annotations for clarity:
TypeScript1import * as fs from 'fs'; 2 3const filePath: string = 'input.txt'; 4const content: string = fs.readFileSync(filePath, 'utf-8'); 5const lines: string[] = content.split(/\r?\n/);
The regular expression /\r?\n/
is used to split the content into lines. It matches both UNIX (\n
) and Windows-style (\r\n
) line endings, ensuring compatibility across different text file formats. Here, lines
is an array of strings, each representing a line from input.txt
. For a file's content like:
Plain text1Hello, 2world 3!
The lines
array would be: ["Hello,", "world", "!"]
after the split operation.
After obtaining your file lines in an array, you can iterate over them using a for...of
loop.
TypeScript1import * as fs from 'fs'; 2 3const filePath: string = 'input.txt'; 4const content: string = fs.readFileSync(filePath, 'utf-8'); 5const lines: string[] = content.split(/\r?\n/); 6 7for (const line of lines) { 8 console.log(line.trim()); 9}
- Looping Over Lines: The
for...of
loop processes each line in thelines
array. You may explicitly declareline
as astring
for clarity:for (const line: string of lines)
. - Using
trim()
: This method removes leading and trailing whitespace, including newline characters, from each line.
The output will display each line from input.txt
without unnecessary newlines:
Plain text1Hello, 2world 3!
Let's extend our skills by reading integers from a file and calculating their sum, utilizing TypeScript's typing benefits. If numbers.txt
contains:
Plain text110 220 330 440
The following TypeScript code computes their sum:
TypeScript1import * as fs from 'fs'; 2 3const filePath: string = 'numbers.txt'; 4const content: string = fs.readFileSync(filePath, 'utf-8'); 5const lines: string[] = content.split(/\r?\n/); 6 7let totalSum: number = 0; 8 9for (const line of lines) { 10 const parsedNumber: number = parseInt(line.trim(), 10); // Convert each line to an integer 11 if (!isNaN(parsedNumber)) { // Ensure the line is a valid number 12 totalSum += parsedNumber; // Add the integer to totalSum 13 } 14} 15 16console.log("The sum of the numbers is:", totalSum);
- Reading Lines: The lines from the file are read into the
lines
array. - Converting to Integers: Each trimmed line is converted to an integer using
parseInt()
, with the second argument10
indicating that the numbers are in base 10. - Calculating Sum: Valid integers are summed up in the
totalSum
variable.
Executing the code, you'll see the total sum of the numbers:
Plain text1The sum of the numbers is: 100
In this lesson, you developed the ability to read a text file line-by-line using TypeScript — a fundamental technique for efficiently processing large datasets. You've learned how to manage file I/O operations with the fs
module, implement content splitting with split()
, and clean data using trim()
. The addition of TypeScript's typing provides robust error checking and improved code clarity.
You're now ready to tackle practice exercises, applying these concepts to further enrich your understanding. As we progress, we'll uncover more parsing techniques. Keep honing your skills with TypeScript!