NodeJS

NodeJS(1) File System (fs) module

오리888 2023. 4. 9. 14:19

In order to read and write files, you need to use fs modules. Let's look at the example right away to see how fs module works.

 

Ex.

So in line 1, you first enable readFileSync, writeFileSync by requiring the fs module.

Then, I made two text files to use the readFileSync function -> and simply put the path of the file I want to read in the first parameter and the character encoding type in the second parameter.

 

After running the code, I can see that the readFileSync function works well.

 

For writeFileSync, it's basically creating a new file and inserting the data you want to put in.

Let's say that I want to creat a new text file in the in the content folder and name it as "test3.txt".

 

Ex.

As you can see in the example, you just put the path in the first param, data in the second, then the character encoding type in the third parameter. When I run the code and check the content folder, I can see that the file has been created.