Let's not look at how we can read and write text efficiently and the sort sort of thing here can be 2
a file or a socket or even a console that is a command prompt in the follow up lessons. 3
People do the most of most of the stuff we learn in this lesson let's begin with file reader unfiled 4
either process. 5
And as the name suggests they are used to handle files that contain text obviously files that is for 6
reading characters while Filemaker is for writing got actors. 7
And here is how they are instantiated. 8
You simply pass a filename to their constructors. 9
Earlier you said that character streams are built on top of byte streams and these classes are built 10
on top of file input stream unfiled stream process. 11
So internally they use these classes to do the actual reading and writing off bytes and they perform 12
the necessary to and fro cancellations between bytes and characters. 13
Now JVM has a default Goudeket encoding for reading and writing bytes on these classes use that encoding 14
and by default JVM actually uses the default character encoding of the underlying operating system. 15
I believe it is C.P 12:52 on Windows annually of 8 on Linux. 16
So it is a JVM configuration and if you want to change the JVM to different encoding you can set it 17
on the command line in this way. 18
Here find out and coding is referred to as a system property of JVM and the option high Fundy is used 19
to indicate that. 20
But if you want to know what encoding your JVM uses you can use either of these two approaches. 21
One is by invoking the get property method in the system class and you would have to pass a system property 22
name find and encoding as a method argument. 23
Second is the default Gobseck matter from the outset class which is from the Djala Dot and I package 24
. 25
You can check out the resources section. 26
Better link is included that has more information on JVM system properties. 27
Generally it is recommended not to use these classes as you have no control on the carpet and coding 28
that is you're forced to use the default setting unless you use the command line option to change the 29
setting so you will be platform dependent. 30
If you are relying on this process so the preferred approach is to use these two classes. 31
Input stream reader an output stream writer and they enable you to set the character encoding. 32
Well if I later unfiled order were specific to files these are general purpose classes for doing the 33
translations between byte and Carkner streams. 34
And hence you can see that the input parameters of the constructors of the abstract input stream an 35
output stream process that is not specific to file streams and hence can be passed any by stream objects 36
so input stream reader will translate from bytes. 37
Look at occurs while output stream writer would translate from characters to bytes. 38
So the benefit of using these two classes is that they are general purpose on modeler. 39
You can set the carpet encoding off your choice. 40
There are also overloaded constrictors and you can check them out from the API. 41
And here is an example. 42
Here we want to read on benzyl for text file. 43
So you must create a file input stream object and then we pass it as an argument to the input stream 44
reader. 45
Here the character encoding is specified as Eudy of eight. 46
Since files are on Filemaker are specific to files they actually extend these classes as these classes 47
are more generic. 48
Also note is that I do here says Michie's and that's because these classes act as a bridge in the translation 49
between bytes and characters and to achieve Dopp efficiency you would grab them with that buffered reader 50
or buffer the writer class which provide additional buffering capability. 51
And here are the constructors for buffered reader and buffer writer. 52
They simply take out after streams reader and writers input. 53
There are also overloaded versions which additionally take the buffer size of them but note that the 54
default buffer size is around 8000 characters. 55
That is it is a Carrere with the size of add on 8000 So these are similar to buffer input stream unbuffered 56
output stream process which are chained to input stream an output stream instances So let's look at 57
the Bufford reader on here and read methods written before Twitter. 58
We all get it is because of too while discussing Rueter. 59
However we now get the additional benefit of buffering just method here. 60
Returns a single character. 61
Well the second one reads a group of characters in the input katak it underlines that amount of character 62
Selecta Turbin is something new and it is probably the most commonly used when reading Cocker's as a 63
matter of or. 64
It simply needs a single line of text unrepented as a string. 65
So if the input character stream has spects with several lines then this method can read on each line 66
separately. 67
That is each invocation rodent's one single line and to identify lines. 68
The matter looks what these lines up added or as a line feed a carriage return or a carriage return 69
followed immediately by a line feed. 70
Also just in case if you are not aware of this line separate or guns they happen to come from the typewriter 71
times where carriage return would signal moving horizontally to the beginning of the same line one line 72
feet would signal moving to the next line. 73
So together they give you the effect of moving to the beginning of the next line. 74
And here is an example of using a buffered reader for reading a text file which is named as go doc. 75
The XTi. 76
So the Cripe answers. 77
We help buffer the reader. 78
Just change to an input stream reader that is intention to file input stream which actually reached 79
bytes from the file. 80
So that's three objects chained together. 81
So file input stream reef's bytes from the file while input stream leader translates those bytes into 82
characters using UAP of 8 encoding and finally buffer reader would buffer those characters so that we 83
get the performance benefit. 84
Outside that block we are creating a string builder that is used for accumulating that text read from 85
the file. 86
So the while loop with each iteration reads a single line of text and we can see that it is using the 87
read line method of the buffer reader object and with each iteration the line that is read is appended 88
to the string builder object. 89
Also notice that we are opening a new line explicitly and that's because Red Line method strips new 90
lines. 91
Finally once the entire text is read the relend method were written in and the loop terminated. 92
And finally we can do it on the string containing the string builder. 93
Also please make sure we use cringle for such cases as using Pless for concatenation would be very expensive 94
. 95
And if the file is very large then it may involve several concatenation operations on here as an example 96
rendered rendering text from a console that has a command prompt. 97
Only change here is in the Cripe parenthesis file input stream has been replaced with System Duggin 98
in is basically a static variable within the system class and it represents an input stream object and 99
we know that input stream reader is a general purpose bridge class and so it can work with any input 100
stream object and the text that the user enters in the console will be captured by the read line method 101
. 102
So in this case the user will be repeatedly prompted for input on the news user and does the text start 103
at which point the while loop will terminate. 104
So we look at how we can read dextro efficiently. 105
Now let's see how we can write extra efficiently. 106
Here's an example of using a buffered writer for writing a text file unchaining is very similar to the 107
read operation in the type parenthesis. 108
We have a buffered writer that just change to an output stream loader that is in turn change the final 109
output stream which writes bytes to the destination fine character encoding used for writing bytes is 110
UTF aid here write methods and work on the buffer to write an object with some text pass doesn't boot 111
. 112
You can assume it has a string but it can also be a character in it. 113
And it means that right is an overloaded method. 114
In fact this notion of the right method is actually inherited from the writer class. 115
You can take a look at the API of this class to get more understanding of it. 116
So that's about it will get Halvard can't read dextro efficiently from an input stream. 117
And also how we can write text efficiently to some output stream. 118
Next we are willing to do one AACo the most good test all the stuff we learned in this lesson. 119
Thank you