java file streaming
TRANSCRIPT
-
7/21/2019 Java File Streaming
1/17
Java File StreaminISTE101
-
7/21/2019 Java File Streaming
2/17
Character Based File I/O
There are a number of classes e loo! at" Bu#ered$eader class
" File$eader class
" %rint&riter class
" File&riter class
-
7/21/2019 Java File Streaming
3/17
Character Based File I/O
&e also loo! at the corres'onding methods" readline()
" 'rintf()
" 'rintln()
" 'rint
&e ill also loo! at the string format to format to a number of decim
-
7/21/2019 Java File Streaming
4/17
File$eader
&e use the *ava+io+, librar-. Import java.io.*
- FileReader used to read contents of a le as characters. Intended to read tet
. enerall- one character ma- ma' to a number of b-tes
. 2aries based on encoding scheme
. File$eader assumes default encoding scheme (3TF4 Universal Character
Transformation Format). 5''lies default decoder 6 not to be used if non"3TF encoding has been u
-
7/21/2019 Java File Streaming
5/17
File$eader
eneric Eam'le. Character re'resented b- integer value. $ead method return int value of Char
. If read() return "17 no more data to read+
. &e can 'rovide the le or full 'ath
File$eader is not ver- e8cient
It reads one character at a time
Consider 9ouTube ": delivering one 'ac!et at a time dis'la-ing it the running of to fe'ac!et
&ould it not be more e8cient to bu#er and then load;
-
7/21/2019 Java File Streaming
6/17
Bu#ered$eader
Bu#ered$eader or!s in con*unction ith File$eader5cts as a ra''er to File$eader
!B
-
7/21/2019 Java File Streaming
7/17
%rint&riter ? File&riterThe %rint&riter class enables -ou to rite formatted datunderl-ing &riter+
" underl-ing riter eam'le is File&riterThe File&riter class ma!es it 'ossible to rite a le as acharacters+
-
7/21/2019 Java File Streaming
8/17
@ets see ho save or!s
&e need to create a le to save to 4 +csv//Ao do e save the data to a le called +csv
'rivate nal static String @OFI@E D+csvD
&e start o# b- creating a String called @OFI@E and ma!ing it nameans no changes can ever be made to it and it is nal
-
7/21/2019 Java File Streaming
9/17
Ao do e call the save()method
3sing an action@istenerif( actionOb*ect+euals( *bSave ) )G
save()
H
-
7/21/2019 Java File Streaming
10/17
&hat does the save() methodo;
'rivate void save()G // O'en the le ith 5''end %rint&riter out'ut ne %rint&riter(ne File&riter(@OFI@E7tr
out'ut+'rintf(Ds7snD7 *tfField1+getTet()7 *tfField+getout'ut+close()
H
Aere e see ho %rint&riter and File&riter are used in con*unction
-
7/21/2019 Java File Streaming
11/17
Ao do e load and dis'la-;
Start ith a handler that tells us hich record is active in a a-7 or private int displayNum= 0; // !i"! re"ord to display to us#andler
&hen data is read from a le it is read one line at a time
So e need to store it in an arra-+
'rivate 5rra-@istKString: logFile
-
7/21/2019 Java File Streaming
12/17
Ao do e trigger the loadin&e use an action@istener hich calls a 'articular method
loadFile() is our method and e 'ass the +csv le to it and the arra-created in the 'revious slide
Something is returned b- loadFile() called num@oaded
-
7/21/2019 Java File Streaming
13/17
&hat does the loadFile() metdo;
&e ant to !no ho man- records or lines ere loaded
If nothing is read then num@oaded remains as "1 (error) or it is euarra-list si=e
-
7/21/2019 Java File Streaming
14/17
@ets loo! at hat the dis'la-method does
-
7/21/2019 Java File Streaming
15/17
&hat about Let and %reviou$ecords
dis'la-Lum is used as ahandler+
$emember that theloadFile() method setsdis'la-Lum variable to 0at the start
&e rst call the
get%revious() methodusing an action listener
The dis'la-() function iscalled each time to 'arsethe corres'onding indeof arra-list
Then s'lit it ith M7N
delimeter
-
7/21/2019 Java File Streaming
16/17
@ets loo! at hat the dis'la-method does " again
-
7/21/2019 Java File Streaming
17/17
String Formatting'i /
String - String+format( D+PfD7 'i )