Java: Read Text File Easily

43
3



☕ If you want to learn to program, I highly recommend the audiobook “Computer Programming Languages For Beginners By Adesh Silva”. You can get it for free here: audibletrial.com/alexlee ☕

– I show you exactly how to read and write text files, line by line in java! ✅ Simply write this code to open the file and get the text from it line by line using a scanner.

Learn java in just 13 minutes:

The text file I used:

Java made it easy for reading from files and writing to files. First, open the file by creating using File(“path/to/file.txt”); then scan through the file line by line using Scanner to read from the file. If you want to write to a new file simply use FileWriter(“”path/to/newfile.txt””);

You’ll get the hang of reading from files and writing to files in java! They aren’t too bad 🙂 To read file line by line and to write a file in java are very similar. There a few quirks like importing the libraries and adding the “Exceptions” but once you get those and the paths right, you should be good to go!

💻 Source Code:

I hope you enjoyed this tutorial on java file reading and writing! I like to have a nice mix of tutorials and actual projects for you all 🙂

Do you want to learn how to code from scratch, in Java? Check out my Java Tutorial For Beginners:

Was this helpful for you? If you have questions about reading files in java or writing files in java, please ask in the comments, there are a lot of people just like you –

7 Tips on How To Think Like a Programmer:
New Programmer Support:
Java Students Unite:

10% off merch:

Recommended Channels to Subscribe to:
Career Karma:
TheNewBoston:

Camera:
Tripod:
Light:
Mic:
Keyboard:

~
(2nd channel)

Alex Lee

Nguồn: https://thegioiinuv.com/

Xem thêm bài viết khác: https://thegioiinuv.com/lap-trinh-php/

43 COMMENTS

  1. Hey Alex! I wanted to ask while making a banking system project using this scan.nextLine() . How can we store data of multiple users in a file and while reading , it reads the data of that particular user. Guide Plz!!!!!!

  2. Wow, thank you so much, Alex! I wish I was aware of your YouTube channel before! It's so much easier to understand now! Post more videos!!!

  3. Prefer StringBuilders to string concatenation, things that take linear time take quadratic time instead.

  4. What is the file name? The program couldn't find it. I entered my file name correctly and it didn't work.

  5. this was very clear and helpful. Thanks. Can you explain how to read THE LINE from the file. Let say that I want to have resources file, where I store all URLs that I will use in my Java project. How do I read exactly the line that I need in certain place in the code? thanks in advance

  6. Hey Alex ! I had a query what if i wanted to print out a certain line of the file could i still use the scanner class and if yes how or should i be using fileReader.
    Thanks !! Love your videos….

  7. hi alex thanks for the vid , but i wondering if you can do another vid on the same topic but traying other ways of declarations and packages like for example , the printwriter and scanner with (new file(" ") ) constuctor

  8. alex , you can stack ur desktop with 1 click , right click on ur desktop and press " use stack" , ur desktop messy bro .. tq for tutorial btw!

  9. The fact that reading and writing takes an 8 minute video proves how horrible java is. But I swear your video beats my professor's video lesson by whole 45 minutes, and now I actually get it at the end.

  10. It is hilarious to me that my computer science teacher spent an hour and a half lecturing of files and I didnt understand a single second of it, and then after watching you actually work in java for 7 minutes I understand.

  11. Is there a way to make the directory work without all the user/ stuff? Like if it were in the same folder? So that I could send the folder to someone else and still have it work? Or is there a way to automatically get the location of the current java folder as a string then altering the string to direct to the correct file?

  12. C:\Users\pankaj\Desktop\test.java

    if you are working on windows with VS Code to practice this code then make sure u use double bacward slashes "\" while giving the path like I have shown above or the editor shows error.

LEAVE A REPLY

Please enter your comment!
Please enter your name here