How to Read and Write to Text Files in JavaScript

  Рет қаралды 86,353

SteamCode

SteamCode

3 жыл бұрын

Hello everyone, welcome to SteamCode! In this video, I will show you how to read and write to text files using JavaScript. If you enjoy the video, please like and subscribe, and if you have any questions, do not hesitate to leave them in the comment section below.
✅ Check out our Discord! / discord
👉Subscribe to SteamCode! / @steamcode4441
👉Github: github.com/SteamCodeYT
👉Patreon: / steamcode

Пікірлер: 94
@isimlikullanc2638
@isimlikullanc2638 4 ай бұрын
No waste of time, no bullsht, just direct explanation of point. Thank you.
@nyitigvilaga
@nyitigvilaga Жыл бұрын
Hi! Sorry, but this is NOT Javascript! This is Node.js. I'm looking for a vanilla javascript solution. Do you have a solution for that?
@shanermahmud1086
@shanermahmud1086 Жыл бұрын
Thanks a million! This is a great tutorial!
@steamcode4441
@steamcode4441 Жыл бұрын
Thank you! I'm glad that you found it helpful!
@loydcose2780
@loydcose2780 2 жыл бұрын
That was short and simple, Good job!
@steamcode4441
@steamcode4441 2 жыл бұрын
Thanks! I'm glad you found it helpful
@Jandhyalas
@Jandhyalas 2 жыл бұрын
Thank you for a short and simple tutorial.. I am new subscriber to your channel🙂
@steamcode4441
@steamcode4441 2 жыл бұрын
Thanks you! I am glad you found it helpful
@SuperYtc1
@SuperYtc1 Жыл бұрын
How do you read and write files with Javascript without Node.js?
@user-wo4ru6zn9f
@user-wo4ru6zn9f 2 жыл бұрын
Thx man, thats what im looking for
@steamcode4441
@steamcode4441 2 жыл бұрын
I'm glad you found it helpful, thanks for watching!
@SamirJzVFX
@SamirJzVFX Жыл бұрын
God bless you man thank you!
@bhaveshgudlani716
@bhaveshgudlani716 Жыл бұрын
can we add some text at some specific point in the file? like on index 3??
@mambogeezy9317
@mambogeezy9317 2 жыл бұрын
i need help on how one can read multiple files of same type in a folder then after reading them then create one file which comprises of the data which have been read
@shardulsant4726
@shardulsant4726 2 жыл бұрын
Thanks @SteamCode for great content. I had one question though. Recently I was trying to read cert files using similar approach, although when the react project was up on web-dev-server, I was not able to read those files. Is there any restriction with this approach such that when the project is up on some server wr can't read those files?
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Shardul, thanks for watching! Yes, I believe that there may be some sort of restriction depending on the server you are on. It could be possible that the configurations of your environment prevent file system access or it could be a restriction within Node.js itself. Frankly, I do not have much experience in that side of Node.js development, so I couldn't give a definitive answer with 100% certainty. You could possibly check the configuration settings on your server to see if that is what's causing the issue. I could also look into this more to see whether this is an issue on Node.js' end. I hope this has been helpful, thanks again for watching
@mooseminddayan4650
@mooseminddayan4650 Жыл бұрын
Misleading. The title should have Node. People creating a client side app will get an error as "require" is not defined! As well as those with no node installed on their machine
@10F2C
@10F2C Жыл бұрын
try{}catch{} require wont throw an error if it doesnt exist
@DominicNweze
@DominicNweze Жыл бұрын
@@10F2C but it dosent work then.
@leonardlamparter
@leonardlamparter 2 жыл бұрын
Only video that works!
@steamcode4441
@steamcode4441 2 жыл бұрын
I'm glad it helped!
@andrewhull3590
@andrewhull3590 Жыл бұрын
Great video
@kezzaman
@kezzaman Жыл бұрын
how do i get the data out so i can use it?
@dotterel
@dotterel 2 жыл бұрын
THANK YOU, SIR.
@steamcode4441
@steamcode4441 2 жыл бұрын
No problem! I'm glad you found it helpful!
@user-ux2he2ib5y
@user-ux2he2ib5y 5 ай бұрын
thank you usince morning was getting wrong and confusing information but now im oky ..thank you so much
@SipChai
@SipChai Жыл бұрын
Thank you so much
@AP--zg5jn
@AP--zg5jn 9 ай бұрын
Thank you man!
@steamcode4441
@steamcode4441 9 ай бұрын
Glad you found the video helpful!
@chepuriabhinay633
@chepuriabhinay633 Жыл бұрын
Hot to set a value from response.txt file to local variable
@Cuber1771
@Cuber1771 2 ай бұрын
Hey, great tutorial. I was wondering if anyone knows how to save the contents of a file as a variable? I tried saving a content variable as data in the block and it didn’t work. I also tried making as a list and adding to it each iteration but that also didn’t work, and no form of just using the data variable seems to do anything. Sorry if this is a rly simple request an I just don’t see it, I’m new with js so any help is appreciated!
@sabuein
@sabuein Жыл бұрын
Thank you.
@horridparlor
@horridparlor 2 жыл бұрын
I'm using React Nodejs, and I'm getting this error from trying to require "fs", : "ERROR in ./src/App.js 9:11-24 Module not found: Error: Can't resolve 'fs' in '/React/server-test/src' webpack compiled with 1 error and 1 warning" Why is this happening?
@kozkiroify
@kozkiroify 2 жыл бұрын
but how do I put the contents of data in a variable outside of "fs.readfile"? in short, I want to read a file, work with it's contents within my program, then save the results in another .txt
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Kiro, If you want to take the the data out of 'fs.readfile', you will need to store what is in the 'data' parameter into a local variable. For example, you can say: var fileData = data; //This is within fs.readfile. From there, you can whatever you want with the data.
@worldlyemo2290
@worldlyemo2290 Жыл бұрын
@@steamcode4441 since the variable is local, the code outside fs.readfile doesn’t take it over. Also, when I’m trying to implement it into a webbrowser application, the browser console tells me that ‘require’ is undefined, since it doesn’t seem to know node.
@jonathanjonathan1751
@jonathanjonathan1751 Жыл бұрын
thanks so much bro
@kurtismelkisoft4309
@kurtismelkisoft4309 Жыл бұрын
How then to use the give in a real program?
@dylan9191
@dylan9191 11 ай бұрын
It seems fs.writeFile() and fs.appendFile() perform the same operation? You overwrote the readFile with writeFile then edited with appendFile. Could we not just continually overwrite the existing file and forget appendFile altogether or is there some other benefits I don’t see?
@minostache4499
@minostache4499 5 ай бұрын
If you don't want to delete the currently existing characters in the .txt file, then you should use appendFile. It's the same as appending to a list instead of assigning a new value to that list.
@abhijithtgcse1685
@abhijithtgcse1685 Жыл бұрын
Thanks for this information bro, can we use switch and case to add and read contents on the text file ? ex: press 1 to add content press 2 to view content press 0 to exit
@steamcode4441
@steamcode4441 Жыл бұрын
Hey thanks for watching! To answer you question, yes, you can use switch statements and then place unique file system commands in each one
@miladbannourah7798
@miladbannourah7798 Жыл бұрын
thanks for the help, but why do i keep getting and error that is saying require is not defined, i have node installed??
@johnschaefer4905
@johnschaefer4905 2 жыл бұрын
I'm having some issues running node with bash terminal. Following your code on my mac doesn't seem to work, but on my friends windows it works with powershell. Do you know anyway around this?
@johnschaefer4905
@johnschaefer4905 2 жыл бұрын
Nevermind, I was able to figure it out by downloading powershell
@leonardlamparter
@leonardlamparter 2 жыл бұрын
@@johnschaefer4905 Cool, you could also write this line of code in an html file: ** that is what I did atleast
@ekoking338
@ekoking338 Жыл бұрын
if in file.txt new row, why get to string bro example in the file txt is beginer medium low higt i get to variable string what code in java doctype script var dat01,dat02,dat03,dat04; load file and maybe like this dat01 this is beginer dat02 this is medium thx bro
@younesqara8082
@younesqara8082 2 жыл бұрын
I'm trying to load a text file so I can use the content as values to my variables. I've tried many approaches, but I think this one is the simplest. However, every time I apply this code in my editor I run into the same problem: ( Error: ENOENT: no such file or directory......... errno: -4058, code: 'ENOENT', syscall: 'open', path: the_to_my_file_here.... I tried to change the extension of my text file but it didn't work, I also tried to change the location of my script and the text file, it didn't work also. I am using : ---VS code ---JavaScript ---file.txt (also tried .csv) ---windows 11 I'm looking forward for your reply.
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Younes, so it seems that this error is being caused because your code is not finding the text file that you're referring to. How does your directory structure look like? For example: If your text file is in the same directory as your JS file, you can reference it like so: "./FILENAME.txt". If your text file is one directory outside of your JS file, you can reference it like so: "../FILENAME.txt". (Note: there are two "." to represent that you are moving back one directory.) When I run into this error, this is usually the cause, so try to check whether your code is referencing the correct directory. I hope this helps!
@younesqara8082
@younesqara8082 2 жыл бұрын
@@steamcode4441 I actually tried this method but it didn't work. so I created a new folder with a new script and a new file.txt only, and it worked. now what I need to understand is why it doesn't work when I have other files in the same directory as my script. the other directory in which the script didn't work contained: ---2 .json files ---1 .html file which is linked with the script.js ---and a node_modules (I don't remember where it came from) I know nothing about json files and I am just getting started with node, so I'm having trouble with this.
@steamcode4441
@steamcode4441 2 жыл бұрын
@@younesqara8082 Hm, the json file shouldn't have been the issue. Try adding files to the working directory and see if it still work
@younesqara8082
@younesqara8082 2 жыл бұрын
@@steamcode4441 I managed to solve the problem by changing the whole directory location.... thank you for your reply.
@jamesharland3727
@jamesharland3727 6 ай бұрын
Thanks for this! Is console.err a thing or was that a typo for console.error(err)?
@steamcode4441
@steamcode4441 6 ай бұрын
Glad you enjoyed it! Yup, console.err was a typo, it should be console.error
@molostv2786
@molostv2786 4 ай бұрын
is there a way to make each appended string be on a new line?
@steamcode4441
@steamcode4441 4 ай бұрын
Yes, you can add the newline escape string " " to the end of each string you want to append.
@ErodonTV
@ErodonTV 2 жыл бұрын
so yummy , ty ty
@user-zv6vl6ne9z
@user-zv6vl6ne9z 2 жыл бұрын
Hi bro..how can i write a .txt file with table ouput format ? for example I have to read a txt file, and i have to make some calculations and write the ouput like this... ID price color quantity total 2 10 blue 3 30
@steamcode4441
@steamcode4441 2 жыл бұрын
Could you use a .csv file? Those are analogous to .txt files and they are automatically formatted into a table format. If you use a .csv file, you could read each row and take out the commas that separate each cell; thus, you'd be able to make a table.
@user-zv6vl6ne9z
@user-zv6vl6ne9z 2 жыл бұрын
@@steamcode4441 Thank you!
@steamcode4441
@steamcode4441 2 жыл бұрын
@@user-zv6vl6ne9z you're welcome!
@mamthavenkatesh9990
@mamthavenkatesh9990 2 жыл бұрын
Hi @SteamCode, Great Video!! I need to send my json data in json.gz file format to api as payload. How can I write my data to json file sample.json, then gunzip it to sample.json.gz ?
@steamcode4441
@steamcode4441 2 жыл бұрын
Hey Mamtha, sorry for the delayed response. I've never worked with.gz files so I won't be able to give you a direct answer. I will be looking into it though. Thanks for watching!
@Wraient
@Wraient 2 жыл бұрын
how can i run a node app on my LAN so that i can use the website within my network. I guess XAMPP is the answer but how does that work?! how do I do that?!
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Rushikesh, I've never actually attempted to work with Node in a local network, so I couldn't give you a definitive answer at this time. I'll look into it and if I find an answer I'll let you know. Thanks for the question.
@Wraient
@Wraient 2 жыл бұрын
@@steamcode4441 I couldn't work it with node so I used django. It works perfectly fine. Anyway thanks for your quick response.
@steamcode4441
@steamcode4441 2 жыл бұрын
I'm glad you were able to figure it out, sorry I couldn't be of more assistance.
@learnmore6151
@learnmore6151 2 жыл бұрын
Am see this errors "Uncaught ReferenceError: require is not defined" please can you give me solution
@steamcode4441
@steamcode4441 2 жыл бұрын
Hey, could you please send your code so that I could see where the issue may be? Thanks!
@learnmore6151
@learnmore6151 2 жыл бұрын
@@steamcode4441 i want to creat a new html file in my host by evry user input . I can maksure the rendom file name but i don't know how can i creat new file
@steamcode4441
@steamcode4441 2 жыл бұрын
@@learnmore6151 You could use the fs.writeFile() method. It creates a new file if one doesn't already exist.
@learnmore6151
@learnmore6151 2 жыл бұрын
@@steamcode4441 thanks i am trying
@catherineragasa9159
@catherineragasa9159 2 жыл бұрын
I've tried using your code but the error 'module not found' always appears??
@steamcode4441
@steamcode4441 2 жыл бұрын
Hm, that's odd. Have you installed fs with npm?
@armedforce12
@armedforce12 2 жыл бұрын
How can we install fs in plain javascript. I am not using node
@steamcode4441
@steamcode4441 2 жыл бұрын
@@armedforce12 You cannot use fs with plain JS because fs is Node.js module. If you want to access the filesystem without Node, you can use the JS File System Access API.
@minhtuan299maxspeed
@minhtuan299maxspeed 2 жыл бұрын
Can i do it in JavaScript without nodejs ?
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Tuấn, Javascript does not have the ability to access the user's file system, so the project need to be run on a server. The method I chose was node.js, but there are other methods such as using an ActiveXObject or using PHP as well. I may make a video on those sometime in the future. I hope this helped.
@minhtuan299maxspeed
@minhtuan299maxspeed 2 жыл бұрын
@@steamcode4441 thank you so much
@steamcode4441
@steamcode4441 2 жыл бұрын
@@minhtuan299maxspeed Glad to help! 👍
@marcussimmons7003
@marcussimmons7003 2 жыл бұрын
require isn't defined
@edwardmacnab354
@edwardmacnab354 5 ай бұрын
yeah right--now tell us how to do it in vanilla js or do i have to go through all the node.js source to figure it out
@devastrapistdontsueme
@devastrapistdontsueme 9 ай бұрын
i have an error: "fs is not defined"
@me_sumit_0
@me_sumit_0 9 ай бұрын
You'll have to make sure that node is properly installed in your machine along with all the necessary packages that are required to have for successfully performing such operations.
@devastrapistdontsueme
@devastrapistdontsueme 9 ай бұрын
@@me_sumit_0 thanks
@hbevt4263
@hbevt4263 Жыл бұрын
fs is not define
@mihakastelic3439
@mihakastelic3439 Жыл бұрын
in termanl write: npm install fs
@muhammadzaakirmungrue3146
@muhammadzaakirmungrue3146 2 жыл бұрын
Dude require does not exist in javascript please change the title of this misleading video.
@steamcode4441
@steamcode4441 2 жыл бұрын
Hi Muhammad, require() exists in Node.js, which is a JavaScript runtime. It was for this reason why I thought it was appropriate to name the video as such.
@muhammadzaakirmungrue3146
@muhammadzaakirmungrue3146 2 жыл бұрын
@@steamcode4441 nice reply , thanks, but you know there is something called plain vanilla javascript es6. My point is "There is something else called node built by Ryan Dhal ." and its not javascript ! That something is called Node and that is where require came from . Javascript uses import and script tags dude !
@steamcode4441
@steamcode4441 2 жыл бұрын
@@muhammadzaakirmungrue3146 I see what you're saying. I might make a video on how to read/write to files using pure JS.
@thechoosen4240
@thechoosen4240 4 ай бұрын
Good job bro, JESUS IS COMING BACK VERY SOON; WATCH AND PREPARE
@liamarnold8819
@liamarnold8819 2 жыл бұрын
not funny
@baconxd2078
@baconxd2078 2 жыл бұрын
If that u can not watch this vid
Read a file using the FileReader API - JavaScript Tutorial
25:48
OpenJavaScript
Рет қаралды 20 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,1 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
路飞被小孩吓到了#海贼王#路飞
00:41
路飞与唐舞桐
Рет қаралды 83 МЛН
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 33 МЛН
Java File Input/Output - It's Way Easier Than You Think
8:18
Coding with John
Рет қаралды 443 М.
Read/Write JSON Files with Node.js
10:27
Hey Node
Рет қаралды 87 М.
8.5: Saving Data to JSON File with Node.js - Programming with Text
17:00
The Coding Train
Рет қаралды 224 М.
Cleaner Code: 3 Ways You Can Write Cleaner Code
7:41
Coding with Lewis
Рет қаралды 80 М.
Parsing a CSV file with JavaScript
21:21
Covalence
Рет қаралды 6 М.
Reading and Writing to a JSON file with Node
9:11
innovati
Рет қаралды 24 М.
JavaScript FileReader
15:08
Kyle Robinson Young
Рет қаралды 137 М.
🤔Какой Орган самый длинный ? #shorts
00:42