Lesson 1
Introduction to Text File Handling with TypeScript
Introduction

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 TypeScript, 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 TypeScript 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!

Working with File Paths

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
  • 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 the user directory in the examples above.

Here's how you can specify a file path in TypeScript using Node.js:

TypeScript
1const filePath: string = '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.

Defining Relative Paths with Examples

When working with relative paths, it's important to understand the structure of your directories. Here are a few examples with file trees:

  1. Example 1:

    File Tree:

    Plain text
    1project/ 2├── script.ts 3└── data/ 4 └── input.txt

    Relative Path:

    TypeScript
    1const filePath: string = 'data/input.txt';
  2. Example 2:

    File Tree:

    Plain text
    1user/ 2├── documents/ 3│ └── script.ts 4└── input.txt

    Relative Path:

    TypeScript
    1const filePath: string = '../input.txt';

    The .. is used to navigate to the parent directory. It works this way in both MacOS/Linux and Windows.

  3. Example 3:

    File Tree:

    Plain text
    1application/ 2├── scripts/ 3│ ├── script1.ts 4│ └── script2.ts 5└── resources/ 6 └── input.txt

    Relative Path (assuming the script is in either script1.ts or script2.ts):

    TypeScript
    1const filePath: string = '../resources/input.txt';

These examples illustrate how relative paths depend on the current working directory of your script.

Opening a File

In TypeScript with Node.js, the fs module is used to open and read the contents of a file. We'll use TypeScript's import syntax to include this module. The readFileSync function is used for synchronous file reading, requiring the file path and the encoding you want to use.

TypeScript
1import * as fs from 'fs'; 2 3const filePath: string = 'input.txt'; 4 5const content: string = fs.readFileSync(filePath, 'utf-8'); 6console.log("Full file content:"); 7console.log(content);

In this code snippet, content stores the entire contents of the file as a string, which you can then process or display.

The utf-8 encoding is specified in this example because it is a widely-used character encoding scheme that supports a large range of characters from different languages. It ensures that the text data is correctly interpreted and displayed, especially when dealing with non-ASCII characters. Not specifying an encoding or using the wrong encoding might result in incorrect reading of the file content, leading to garbled output or errors.

Summary and Preparation for Practice

In this lesson, you've learned how to:

  • Specify file paths correctly with examples from different operating systems.
  • Use Node.js's fs module in TypeScript to read the contents of a file with the fs.readFileSync() function.
  • Define relative paths correctly with examples and file tree illustrations.
  • Understand the importance of specifying 'utf-8' encoding when reading files.

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.