Java IO Basics | Input/Output in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this particular video tutorial and you can say it as a your with note understanding of topic by topic java understanding and explanation here we learn about the java io streams so in this particular tutorial is dedicated as for input output streams of java so as you know that input output stream is supported by java.io package java.io package provides lots of classes and methods which all together supports for reading and writing the streams in java so we learned we learned with the theoretical understanding of what is java io streams how java your streams are how this package java.io package is actually organized what are the classes are there the key in a key i mean the important classes and they are important methods will be explained with programs that's the motto and objective of this particular video so the tutorial will learn about the java input output streams and their types what are the different types of the input output streams are there so first you should supposed to know about the streams io stands for input output so you have to understand about the stream what do you mean by stream over here a stream is nothing but sequence of data that are read from the source and written to the destination so suppose you are going to read something suppose from a file you are going to read some data so it's a sequence of data which you'll read now starting from the first character maybe or from the first byte maybe so that will be known as the java stream and if you want to write some data to the particular java file then also some stream is required to write down the characters or the bytes so there are two types of the streams one is the input stream which is used to read from the data read the data from a particular source and output stream is to write the data to the particular destination the destination may be file or output screen anything so for basically for each and every java program which you have learned till now has maybe a simple function known as the system.out.println so this particular method this particular function is having a output stream one system dot out is the output stream actually but for that java.io package is not required to be imported the reason is that system class is a java.lang specified class and java.lang is a default package which is available to each and every java program so this is a basically a very simple example of java stream which is used to point towards the java display so suppose you are using the display as a monitor so it will points toward the monitor and then it will print whatever is there through the printer and method so you're printing hello world in this simple program i'm not going to execute this program and show because i hope that you know each and everyone knows these things we are you are not learning java first time you have come up to the level of java.io so this is what the basic example system.out is the type of output stream so how it happens actually there will be a source each and every java e operation will be having a source and a program will read the data from the source similarly a program will output something to the destination the destination may be a file or it may be the output screen or it may be a stream yes so this is how the reading and writing happens in java reading and writing happens in this way so we'll see that what are the different type of the streams are there there are two types of streams available in java now come where we are getting in depth so listen to this uh lecture a bit before we go to this eclipse and execute all this program one by one we have lots of programs okay we execute all this program one by one and execute you and explain you line by line but we should suppose to know a bit of theory also before we go to these practical programs and explain and execute and all these things so there are two types of the streams you are coming to the theory one is the byte stream another is the character stream so the names are very clear byte stream means the data which will be read or or written which will be read or written in the terms of the byte byte means eight bit is equals to one byte and the character stream the name is very clear character stream means character by character it will read or write the data so we'll see about the byte stream class by stream method now that how the byte stream works it will read and write the single byte of data definitely keep one thing in mind there are two classes this you should supposed to know one is the input stream another is the output stream now we are going to the practicality actually how to implement that so we have a class in java.io package one name of the class is input stream which which deals everything related to the byte stream i mean this is the super class this is the top in the hierarchy class to deal everything about the byte stream input similarly output stream is another class which is defined in java.io package which deals everything regarding the writing bytewise into the output i i mean the bytestream writing purpose output stream but these two classes are abstract plus so you cannot create object of input stream or output stream rather it has many subclasses you can create the reference of these abstract classes and can create the object of those derived classes and assign the derived class object to the abstract class which is the superclass reference variable we'll show it okay with the program if you're not uh getting it clear in your mind at the very beginning at this stage don't worry we'll go to each and everything in detail one by one so you have the java input stream class over here java input stream class input stream class is having a hierarchy this hierarchy we have for input stream the java this input stream class is what it's a abstract class it is extended that is inherited by file input stream class byte array input stream class and object input stream class so if you want to create an object of input stream as it is abstract class it will give an error rather you can create a reference of input stream and can create the object of these classes any one of these classes and assign the object to this particular abstract class reference in this way we can deal with the input stream so input stream is a very important class for byte you know for byte stream reading so it has these three important classes as i've explained actually many classes it has this important things are these the important classes which are defined inside the input stream class so how to create an input stream class it's very simple thing you cannot create you you you cannot first of all you should supposed to import the package it's in the java.io so you have to either write import space java dot io.statics as i have provided over here as you can provide over here java.io.astrox that is star marks you can put then all the classes will be imported or to learning purpose as the first time a beginner programmer also you should suppose to know which classes are required and which classes are being used so you can write down the proper file name in this way and the class convention in java is that for class name we write write each starting words later as the capital so i is capital s is capital for file input stream class you can see that f is capital i is capital and s is capital well so how to create an input stream class just to create a reference object one is actually the reference variable of the abstract class input stream which is assigned with the object of file input stream class so you can create the file input stream class object or byte array input stream class object or object input stream class object and assign to the input stream yes the reason i told it cannot create the object of input stream because it is an abstract class so always keep this thing in mind otherwise you will end up trying to create input stream class object and it will be never giving you a pass through the compiler compiler will give a compile time error always so this input stream class has as it is the abstract class it has lots of abstract methods which cannot be which can be easily as as all these all this file input stream byte array input stream and object input stream class has inherited that class so these methods are available to those class objects definitely so these are the important methods we have in input stream class read read will read one byte of data from the input stream read and if you provide in between in the in an argument to the read method the number of array bytes and what will happen it will read the byte from the stream and stores in the specified array so from the particular stream which will be specified by object that will be read and in this particular add it will be stored available it will return true or false yes it will return the number of bytes available in the input stream mark will mark the position where the data has been read suppose this 100 byte file and 50th byte has been read so it will mark on the 50th byte because up to that much it has been read yes similarly reset is a method will which will you know the the control uh what the mark was set now that will be returned by this particular method wherever the mark is set that can be fetched back by using the reset and mark supported is depends on mark and reset both that is it will check that mark and reset methods is supported in the stream or not then it will give true or false result similarly skips will discard actually the specified number of bytes from the input stream and close will close the input stream after closing you cannot read it out so you will see with a small program few of these methods this program will execute in eclipse now and we'll see how it basically works so as you can see here we have imported this java.io.file input stream we'll create the object of file input stream and we'll create a reference variable of input stream because it's an abstract class we can only create the reference as i told before so we are creating a byte array with the new byte value of 100 now we are printing in the output stream at the beginning hello then we are providing to the try block because this input output operations may generate exception suppose you are trying to open the file input.txt if i don't write down this input.txt file then what will happen yes it will give an exception or maybe there are many other reasons which may generate one exception correct so exception is a very uh you know common for i operations i o related exceptions will generate generally prone to be or assumed or predicted to be occurred so what we'll do will always put this i operation inside the try block so this try block started over here and closing over here after that we have the catch block so inside the try block all the input output operations we are doing so we are first doing we are creating an object that is the reference variable of input stream class which holds the object value of file input stream so when you write down file input stream and pass input.txt so this particular file is being pointed out yes this particular file is being pointed out now we are writing by using system.println available bytes in the file how much files are available input dot available so it will give the it will give you the particular number of bytes available 46 bytes are available it has red i'll show you how to do it i mean how i'll execute and show it to you now input dot read array so what we are doing basically this array here reading here so this many will input from the input that whatever the documents are there whatever the values are there in input.txt will read from there and put into the array so 100 bytes is the space now so it is if input.txt has less than 100 then it will fit to this array then we are printing data red from the file are you clear now we are printing we are creating a data object of the string type by using the string new keyword so we are creating a string named data with the value of array whatever the value of array we have that is the content of input.txt that is being faced and created into the string data now the data is being printed over here and after that the job is done so we are closing the input string so what will be done here first what will be the output first we are printing hello after that we are opening the file and you're printing this available bytes in the file so what is the available bytes in the file this input file so these are the bytes four five six seven eight nine ten so it may be 40 to 50 around so that will be being printed if you execute this it is 46. after that it will print what data rate from the file that will be printed and then we will print the data itself data itself means the entire line will be printed whatever things are there inside that will be printed see we'll execute and show it to you we'll execute this program and show in front of you yes see this hello is first printed and then 46 bytes are there then data read from the file and this is the line of text inside the file this content of the file is being faced we'll change the content and see hello guys we are changing the content yes my typing speed is not bad well so i'll execute this yeah you can see the change now so it is being happened in these two lines yes the input is actually read by using the read method and put into this array and that array is giving the this string data because we are creating in this way and then this string is being printed over here see this my speed of writing is fast but i've done some spelling mistake also here typing mistake we are changing n is missing n is missing here well so this way uh input stream actually works so this is the program the first program we have seen over here regarding the input stream now we'll comment it out now we'll see the next program with the output stream so i'll not go to the pdf okay in the pdf all these programs are available so and the pdf will be available in the description of this video so that you can learn it completely so output stream is created over here this class and file output stream is also not created solely it's important both the classes are imported over here i have specified the name just because to show you that we know that which file working on otherwise you can just provide asterisks then also it will be all working well so all classes will be imported right now we are writing some we are taking some string name of the variable is data in this string we are writing that this is a line of text inside a file so this is the strings content now you are creating this here also you can see that all the i o operations we are putting inside the try block because uh it generates any exception now we are creating an object of output stream by creating new file output stream file output stream is a class which deals with the file system okay with the files we did is the stream of which stream uh reading and writing that is the as is the output stream writing using a file so we are dealing with the file right so that's why we're using file output stream otherwise we have many other classes three important classes as shown over there a particular class can be chosen based on the particular function here i'm giving an example with the file only so output.txt output text is a new file being created here are you getting now this byte array is created in the name of the byte array is data bytes now in the data bytes we are i mean writing this thing data dot get byte so what will be there tell me if you think it i think it what will be get bytes the method the method is a string specified method right get byte it's available in java.lang.string here's this thing clearly now it returns byte array see this signature is provided over here so it's a string specific method so this method will return the number of bytes which is available in the string data so 1 2 three four five six seven eight so if you even to go through maybe some 35 to 45 whatever the number of bytes are there that is being returned so you can understand that data bytes consist of the number of bytes which is available in this particular string named data now we are writing over here out you are using this object output stream object dot right right is a method defined into the output stream see java output stream class and here we can see that the important methods available output stream also similarly we have it's an x it's an abstract class and file output stream is a class which we which which is which is inherits the output stream and it deals with the file rated operation so we are using these particular examples we have also biter output stream object output stream and all other many other classes which inherits this parent class parent abstract class so output stream we are going to create an uh we have we have been to this particular program so why you come back to this pdf is to show you that this write is a method which writes the specified byte to the output stream and you're using that and if you provide you over here the array it will write the byte from the specified array to the output stream so this address content will be taken and it will be printed into the output screen so that's what we are going to do this address content is being printed into the output stream then you are printing what data is written to the file yes the get bytes has been taken this particular content and then we are printing into the output stream by using what right so if you execute this what will be displaying nothing will be displayed see this just this line is displayed data is written to the file data is written to the file only that that's been displayed the question is that how these things are happening what's the use of this lines yes these lines has created one file named output.txt here you can see and inside the file this line will be there this is a line of text inside the inside the file see this this has been written by your program this file is created output.txt and this thing okay i'll change this i'll change this right hello java input output learning students so now if i execute what will happen see output is all same but the content of the output file i hope that it will get changed yes hello java i o learning students is now displayed so you can see that you can manipulate the file using the program by using the java io output stream right now so you can see this pdf and you can learn these things very easily what is the flash uh flush method which which forces like the data which is there in output stream so destination will flush immediately then we have the closed method these are the important methods in the output stream yeah which will close out these are the important things which is available in output stream class we'll see that this particular program is being explained to you correct now we'll go to the character stream now we'll go to the character stream so i'm not going to explain all these things which has been explained to you and you know these things very well now it's been explained also if you're having some doubts then just pause in the video go back to the video and and see that thing again that will be better actually so now we'll see about the character stream characteristic is used to read and write single character of data it's very important actually it reads a single character wise at a particular data so two classes are very important for characteristic one is the reader and one is the writer so for the for for before what was the important thing tell me for the byte stream we have the input stream and output premiere for the byte stream similarly for character we have the reader and writer files which is the abstract and base class for character stream dealing so we have the reader and writer abstract classes so we'll see about the reader class first reader class you know it's also in java dot io on that particular system defined package and it is also abstract super class and many of the other classes many of the other classes inherit that particular class but for realism one of the main i mean the very important class of that category which is read by buffer and we have input stream reader we have the string reader we have the file reader all these are the hierarchy which leads to the root class and the top class and base class reader which is the abstract class so you cannot create the object of the reader we can create the object of this thing by using reader class reference that's possible so we how to create the reader we'll see this using a program and then when the methods will come i'll come back to this particular point so this program was with the file out to stream and that program has been explained to you so i'll commend this and i'll go over here and explain this program to you is the file reader using the file reader program here you'll see this how it basically works so it's in the java.io package we are using two classes reader and file reader what is file already you know it deals with the file related things this this is the file data class we are explaining this part these particular files so these particular files also you can learn one by one you go to the java details go to the any website and execute one one program for each and every other classes then it will be very nice for you actually you will get a very clear understanding of the entire java io package i am trying to make my video as smaller as possible so i'm not going to give examples of each and everything the basic skeleton and the basic understanding i'm trying to provide so that it becomes a foundation for you to learn further regarding the java.io and keep one thing in mind let me tell you the java io package input output in java is not a small thing we have a book on java.io actually there are lots of classes and lots of different operations are there to learn everything we have a complete you know version and a big book is there on java.i o input output by hardware shield complete reference on java input output you can just go through with that and you can learn in depth this is the this particular video is to provide you the basics the skeleton of java input output so now we'll see okay come to the program in line number 82 you can see that in character area has been created with the character value 100 and the reader object is created now using the file leader class because filer class is a subclass operator it's possible so input.txt so you're using the same file input.txt okay this file should be available before you execute this program otherwise you have to create right click over here source then go to the new and then go to the file file then you can create over here you can write down input dot txt or any particular file name that particular file name only have to execute and open over here so you're using the object input for that so what you're doing over here we are writing is there data in the stream then input dot ready if there is some data on this input.txt file then only ready will give you the it's a method which is defined into the reader class okay abstract class reader so radio will give true else it will give false it will return false if there is no data into the stream okay into the character stream into the character so character wise it will read so from the file from the reader file reader object input so now we are doing that thing only input.read array are you clear it's reading the character yes from the input from here in this particular object is reading yes now we have to print the array to get it the job done see now see now the output you see see this now it is giving the true first this line is giving the result true is there data in the stream the result is true because there is some data yes correct now data in the stream is being printed for this line 93 then we are printing this particular line what's that system.printer and array so array value is what array value is the same value of the input object input object has the value of input dot txt so that particular thing is being read one by one character by character and stored into the array named array and we are printing the array over here so line number 94 is writing this particular lines this is the content of input file let's see changing the content reader class testing so you'll execute this program now see this the output reader class testing yes so we are reading from this particular file and fetching in this way so this program i hope that it will be very interesting for you so learning first time i o this small programs will be very much useful for you now we'll see about the few of the important methods which has been provided over here one is the ready method you've seen the example of it read method you can see you have also seen the example of it you can see the read and this particular method read it it will also read from the read from the input stream and store in this array but it has some starting and i mean the starting location from here it will start and this many characters equal to the length will be reading from the string and it will store to the array mark will mark the position and mark reset skip is same like is almost the same methods which have seen in the input stream class so reader using file leader this particular program is shown in front of you just before so you'll see the next class writer class writer class is for character stream writing reader is for reading writer is for writing is very easy to recall and this is the format we have is the abstract class again we have the buffer writer input stream writer it is reader there for the reader class here it is writer string reader it was now the class name is string writer and file reader it was the name now right now the name is file writer in this way the entire thing has been organized yes in this tutorial we will see that how this writer class object can be created and how the things can be done so we'll come back to this methods before uh that we will go to the program we got a particular program with the writer class and see how it works it is very fun actually it's very easy also for you so we have the public static void main over here and we are creating the string data with the value this is the data in the output file this is the data with the in the output file and the try block what we are doing i'm just making it so that it will be easier for you to read it yes the readability will be increased well well well so in the is that the try try block we are doing all the ureter operations why i'm telling this thing again and again is that whenever you do any i operation put into the try block that's better so output object is created of the of the abstract class writer and so there is a reference variable actually of the class writer which takes the abstract which is a solid class file writer's object and it is opening with creating and file name output.txt in this file it is writing by in the object output stream one by one it is writing the character what the character data what's the data this character is one by one it will be written so you can see that the methods output is almost same but the method is different last time it was stream i mean the byte so byte one one eight bit onwards it will just go through right now in this particular reader and writer class what will happen unicode character wise it will read and write so here it will right now it is a writer class last time it was the reader class it was for reading okay so this particular object is created now and it will write that right where data it will write the data to the output so this data whatever data is there this is the data in the output file using writer class that will be written so from operation is from this side to that side that is data will be fetched back to output data whatever the thing is there in the string data that will be put inside the output and output will put inside the output.txt file so right now the output.txt file has the output hello java i o learning student it was the it was the output of the last program right now if i execute main.java then what will happen this data will be put inside this is the data in the output file using writer class that will that should supposed to be put it will print in the output screen nothing because you are nothing printing nothing just you you're doing with the file see see this how the output has been changed this particular we will see again we will change it and we'll see again hello experimenting so it should suppose this print the output file hello experimenting if we run this program and if it runs successfully then i'll go to the output class hello experimenting has been displayed if you can understand that it's working actually this reader and writer class how it works i have shown you using this particular program and go back to the pdf we'll see about this methods so this method you have seen just now write method correct now how to put this this whatever the content of this area is there that will be that will be written to the output stream of characters which is we will be using the object of the writer plus any subclasses similarly this particular data will be read or will be written over there this particular character will be appended and it will flash will do the same thing and close will do the same perspective so this is the basics about the java io yes this program is explained to you this is all about the java i o now the question is that what's the difference between byte stream and character stream so these doubts i need to make you clear to you that's why i put this particular example by stream is a is a mechanism that performs input at output of 8 bit bytes its internal mechanism only internally it will read and write through 8 bit 8 bit of data so 1 byte each and it's a mechanism of dealing with the 16 bit uni code so characters yes and it performs input and output operation of 8 byte 8 bit bytes and here it works with the 16 bit unicode common classes are file input stream and file output stream here it is file reader and file writer as you've seen into the program so these are the basics about the java io streams this particular pdf will be having all the programs which i explain it to you so that you can learn it later you can make it as a note you can take it as a note also hope that it will help you out in learning if you feel that this learning strategy of academia fine learning is helping out and you are really learning a new topics in a very nice way it's adding value to your i mean the reading capacity study capacity and making uh some somewhat value to your life then please subscribe to my channel thank you thank you very much
Info
Channel: Academy of Fine Learning
Views: 98
Rating: undefined out of 5
Keywords: #JavaIO
Id: nNti3lzU10U
Channel Id: undefined
Length: 32min 12sec (1932 seconds)
Published: Thu Sep 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.