All language subtitles for 005 Reading and Writing Files.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

This is a really exciting lecture

cause you're gonna learn how to read data from files,

and also how to write data into files.

And we already imported the built-in module

that we need for that in the last lecture,

and so let's now get rid of this code

and start using the FS module.

So we use fs.readfilesync, okay.

And sync stands for synchronous,

and you will start learning about synchronous

and asynchronous right in the next video, okay.

So this is the synchronous version of file reading.

There is also an asynchronous version.

So, the read file sync function takes two arguments.

The first one is the path to the file that we're reading

and then also the character encoded.

So, first the path to the file,

and the file is in the txt folder,

and it is the input one.

So, this text that I have here about the avocado

is what we're gonna read into a variable.

And I'm not sure if you're gonna be able

to see this emoji here right on your operating system,

but if you're on Windows 8 or Windows 10

then that's probably going to work just fine.

Okay, so we specified a path there,

and there are multiple ways of doing that,

but for now I'm gonna go with the simplest one.

So, all I'm gonna do is set it in the /txt/input.txt.

So again, we're starting at the home folder,

which is basically the folder

where the index.js file is located,

so that's the dot in there,

and then we move into the txt folder,

and from there input.txt.

Then here the second one we have

to define the character encoding,

which is utf8, usually,

at least if you're just using English.

Okay, and if you don't specify this,

we get back something called a buffer, okay,

and that's not really what we want.

We really just want the text.

And so, calling this function here will now read the data

from the file and return it to us,

and so we need save that somewhere

and we put it into a variable.

So let's say textIn because it's a more text input.

Okay, and then let's log it to the console

just to see if it works.

Give it a second, and now we need to run this here again.

So in the terminal, I can just use the up arrow

to run the previous command.

So here it is nodeindex.js again.

Hit return, and let's see, and indeed here we go.

So here is the content of that file.

So, perfect.

We now know how to read stuff from files.

Congratulations.

Okay, but we also want to know how to write to files.

So let's create some new variable here with some more text,

and then write it into a new file.

So let's call that one textOut, so for output.

And basically let's just write a string

where we will then include this text in.

I'm gonna be using a template string here,

and at this point I'm gonna assume

that you're kinda familiar with the es6 syntax, okay.

So we already used const here,

which is an es6 way of declaring variables instead of var,

and now the template string, which is another es6 thing.

So before the es6, if you wanted to add something

to text in, you would have to use the plus operator.

So let's say, "this is" and then space and then text in.

Okay, so you would have to use the plus operator,

but if it's a template string it's much easier.

All we have to do is use the backticks, then some text,

and then into this string you can

very easily plug in the variable.

So let's say, "This is what we know about the avocado."

Then we use this syntax to input the variable, okay,

and so basically inside of these curly braces here

we can write any JavaScript that we want.

So it's not just for plugging in variables straight away,

we can also do a lot of calculations

or any javaScript expression that we wanted in here.

Now, let's say a new line character.

So that's a new line, okay.

And just to show you that we can actually use JavaScript

inside of these curly braces here.

So this dollar sign and then curly braces.

So date, or actually date like this, dot now.

Okay, so that is our string,

which has this text here in together with the text

that we read before from the variable.

And so now let's write that to a new file.

So again, we use the FS module,

and this time, writeFileSync.

Okay, and again we specify the path to the file,

and we still want it in the txt folder,

and we call it output.txt, okay.

And now we have to actually specify

what we want to write into that file, right,

and tell us the text out variable.

And this doesn't return anything meaningful

until we don't save anything to any variable.

All we do is to finally log something more to the console,

like just informing if the file has been written.

Okay, so let's test it out again.

I'm gonna clear the console with command K.

Hit the arrow up key, enter, and maybe you saw it.

We here now have this new file called Output,

and if I open it up, then you see,

"This is what we know about the avocado,"

which is what we wrote in the index.js,

and then this is the text that came

before from the input file, right.

Then the new line that we created,

and then created on this date basically.

So this is date.now,

so it's just a timestamp in milliseconds.

All right, and this is how you read

and write to and from files in Node.JS.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.