File Handling in Java | Reading and Writing File in Java | Java Training | Edureka
Video Title: File Handling in Java | Reading and Writing File in Java | Java Training | Edureka
Hey everyone this is Neha from Madhu Rica and I welcome you all to the session on file handling in Java let’s have a look at the agenda for the session first I will tell you what is file handling in Java next I will talk about what is stream and also I will
Tell you what are the different types of stream that are available and moving further I will talk about the various file methods in Java that are necessary to perform various operations on file and after that I’ll discuss the file operations that is creating a file getting file information really from a
File writing to a file and many more so I hope you found agender interesting now without wasting any further time let’s get straight into the module first let’s understand what is file handling in Java so file handling implies how to read from a file and how to write to a
File in Java and Java basically provides a basic input/output package for reading and writing streams and also Java dot input/output package allows you to do all the input and output tasks in Java so in order to use a file class you need to create an object of the class and
Specify the filename or directoryname as shown below so first you will write import Java dot IO dot file that is used to import the file class and then you have to create a object of a file and specify the file name simple so let me tell you one thing Java uses the concept
Of stream to make input and output operations on a file so let’s now understand what a stream in Java so the stream is a sequence of data it can be of two types byte stream and character stream now talking about the byte stream it mainly incorporates with byte data when an
Input and output process happens with the byte data then it is called the file handling process with byte stream now talking about the character stream it is a stream which incorporates with characters when an input and output process happens with a character then it is the file handling process with byte
Stream so these are the two types of streams that are available now let’s move further and have a look at the various file methods that are useful to perform Java file operations first can read this method is used to test whether the file is readable or not next can’t
Write this method test whether the file is writable or not and next you have a create new file which creates an empty file and next delete this command is used to delete the file exist this test whether the file exists or not and next you have get name which is used to
Return the name of the file and you have get absolute path and this is used to return the absolute path name of the file and next you have length and it returns the size of the file in bytes now moving further you have lists this method is used to return the array of
The file in the directory and you have mkdir which is used to create a directory okay now these methods are used to perform various file operations now let’s see what are the various file operations that are present in Java so first you need to create a file then
Once you create a file you need to get the information out of the file after that you have to write the information to the file and then you have to read the data from the file correct so now let’s understand all these things in a much better way here with the help
Of an example first create a file in this case in order to create a file you can use a create new file method ok and this method returns true if the file was successfully created and it returns false if the file already exists ok so here is an example for creating a
File so first I have created a package and inside that I have a class call create file as I have already told you I need to import the file class so that is the reason I’m using this package ok and next in order to import the input-output
Exception class to handle the errors I have Java dot input/output dot IO exception okay so next after that I have created a class called create file and inside the main method I’m using two blocks a TAS try and catch so inside the try block what I will do I will write a
Code that has to execute and in the couch block so if there is any error that is going to occur in try block it will be handled and in this case most expected error can be the IO exception so that is the reason I’m using a catch
Block to handle that ok so inside the try block let’s see what I have written so I have written file my object as equal to I am creating a new file in the specified directory if I don’t give the path if I just give the name of the file
That is file F 1 dot txt it will by default go and store this file and the location where my Eclipse workspace is so I don’t want to do that I want to create a file and a specified file location and that’s a reason I’m giving
This and now if my obj that is my object dot create new file then if the file is being created with this object then either selling file created and get the object name let us get name so get name will be what file F 1 dot txt right else if the file already
Exists it will say file already exists and if there is any exception it will be handled in the couch block so now let’s run and check the output so you can say that it’s telling fight created and it returned the name of the file that is my obj dot get name that
This file FN dot txt correct so yes that’s how it works now let’s cross verify this I’ll go to my D Drive I will go to file handling and you can see and you can see file F and creator correct so in this location itself I have
Created the file so if I run this program one more times let’s see what happens so it’s a link file already exists because one time you created a file it will be present in that particular location so it will say file already exists all right so this is how you need to
Create a file so the next step is to get the file information so here again I am importing the file class and then I’m creating a class called file information and inside the main method I’m creating a new file okay and I will check if this already exists then
It should print the file name get file name that is my obj dot get name and then it should again print the absolute path of the file and for that I’m using get absolute path method and then I am using if it is writable it will print
Canned right if the file is readable it will say canned read and the file size and bytes will be my obj dot length okay else if the file does not exist then it will print file does not exist so you can see that I have used all the methods
That I have explained you before that is get name get absolute path can read/write and length right so let’s run the program and check for the output so you can say that the file name is file one dot txt the absolute path is it’s in this location that I have
Already showed you just now for writable and breathable it’s telling true because yeah it is readable and writable and the file size and bytes is zero so I have one more file here let me show you that I have a file called new file one dot
Txt ok so inside that I have written some text so it means it consists of a text right so when I say the file size and bite you have to return the length but here it is telling zero because I have not written anything into that file
So now if I change the file name let’s see what it will return for the statement let me run and check it once again so you can see that it returned the filename absolute path it is readable and writeable and the file size in bytes
Is 50 to correct so if you have written anything in the file then it will return the file size or else it will not simple so this is how you need to get the file information next let’s see how to write to a file so here I have created a class call right
To file on inside the main method again I have to block suckers try and catch and Here I am using the my writer method so here I have used the file writer class together with it’s write method to write some text into the file let’s now understand how actually it works so I
Have used a file writer and I’ve created object of the writer and this is a path where I have saved my file and this is where I want to write some data into the file right then I’m using my writers write method to write this particular
Context in the file and after that I have to close it so once I close this I will say successfully wrote to the file okay if the data has been returned to the file then it will say I have successfully written the data into the file else it will throw an error and
Will be handled in the catch block let’s now run and check the output so if sterling successfully wrote to the file now let’s cross-check this so you can see that it has written the data into the file APIs Java is a prominent programming language of the millennium
If you want to do any editing over here you can do which means it is writable and if I want to read the data from the file I can read it as well right correct see so simple it will even ask whether to save or not if I say save yes
It will get saved very simple right so that’s how you need to write a data to the file now that you have written the data to the file you know that you can go to the particular file location and check whether the data is being written
Into the file or not but how does Java know that right so now let’s write a Java program to read the data that you have written into the file again I’ll create a class called read from file and inside the main method I will write file
My obj is equal to new file and this is the path of my file and then I’m using scanner and my reader by because I want to read it that is the reason and I’ll pass the parameter as my obj my obj refers to the file which is this one
Right and next while my reader dot has next time so if it has next line or if there is n number of data that is being present than that it will read everything that is the reason I want to check whether it has next line if s
Again string data is equal to my reader dot next line and it will print the data and after everything it says my little dot close because I have to close this and finally if the file has not found then it will throw an error and will be
Handled in the catch block simple now let’s run and check the output so you can see that at return Java as a prominent programming language of the millennium which I had written from this thing right so from the right method and this I manually type so it will again
Print the data that has been present so this is how you need to read the data from the file I hope you understood how to create a file how to get the information from the file how to write to a file and how to read from a file so
These are the various file operations that you can perform on a file so that’s how it works and that was all about the file operations where this we come to an end of the session on file handling in Java if you have any queries you can comment in the comment section below and
We will reply back to you at the earliest thank you for attending this session and happy learning I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the
Earliest do look out for more videos in our playlist and subscribe to any Rekha channel to learn more happy learning
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Got a question on the topic? Please share it in the comment section below and our experts will answer it for you. For Java Training Curriculum, Visit our Website: http://bit.ly/32Ymvde
that "ok" was so cute 😂🥰
Clearly explained
Please share the code
thank you
Thank you so much ma'am 🙏🙏🙏
helpful video, clear and awesome. thank u.
i understand everything but when I compile I get a few errors…
why did u not get an error when u used a scanner without importing the utility.scanner…(which i fixed)
also, another error saying it says it can't find the symbol "FileNotFoundException e"
other than that i understand perfectly fine..its just java giving the catch error
Edurekha is my favourite!
I have a query. When I have a list of movies in a text file and I try to read the file and display the content in java output console. how to print them all line by line instead of them getting displayed in a single line like this (the shawshank redemptionthe godfatherthe dark knightschindler's listpulp fictionthe lord of the ringsthe good the bad and the uglyfight clubthe lord of the ringsforrest gumpstar warsinceptionthe lord of the ringsthe matrixsamuraistar warscity of godthe silence of the lambsbatman beginsdie hardchinatownroomdunkirkfargono country for old men
). Any piece of advise?
Thank you Mam. I ran all the code and it worked successfully. I am feeling confidence after seeing your video. God bless you for your work. Thank you.
I have Question
if user want to create a file
but not in time of object creation
in time system input gives input name
example: if want create file when i give the name in console
THankyou so much .Its very easy to understand the concept with You.Thankyou:)
Very nice explanation. Thanks for this video !! Worth Watching👍
Thanks 😉
Helpful 👍
even though its vast topic, given overall idea which concepts needed to learn👏👏
thank you so much for such an awesome tutorial
I want to add something to the video: for writing to a file, right now how it is set up in the video it will delete the contents of the file and write the last thing you told it to write. if you want to append (add extra) to the file, in your FileWriter, u need to add second parameter true so it will append: ie FileWriter myWriter = new FileWriter("Filef1.txt", true); this way whenever u run the program it will keep writing to the file every time you run it instead of deleting the contents of the file.
Thanks allot✨ well explained but delete operation is not been explained …. Can yuh plz make a video on that also
Really helpful, thankyou
Superb.
This 13 minute video was better than 2 hours at my university.
you explained it very beautifully.
Incredible explanation, you resumed multiple lectures of my Computer Science course in just few minutes. Thank you for making this simple to understand with this awesome example.
what is the use of 'Scanner' use here?
helped a lot, thanks
Thanks so much
Thank you, Edureka!