Java File Streaming
ISTE101
Character Based File I/O
There are a number of classes we look at
- BufferedReader class
- FileReader class
- PrintWriter class
- FileWriter class
Character Based File I/O
We also look at the corresponding methods
- readline()
- printf()
- println()
- print
We will also look at the string format to format to a number of decimal places
FileReader
We use the java.io.* library
Import java.io.*
- FileReader used to read contents of a file as characters
Intended to read text
Generally one character may map to a number of bytes
Varies based on encoding scheme
FileReader assumes default encoding scheme (UTF: Universal Character Set
Transformation Format)
Applies default decoder not to be used if non-UTF encoding has been used
FileReader
Generic Example
Character represented by integer value
Read method return int value of Char
If read() return -1, no more data to read.
We can provide the file or full path
FileReader is not very efficient
It reads one character at a time
Consider YouTube -> delivering one packet at a time displaying it the running of to fetch another
packet
Would it not be more efficient to buffer and then load?
BufferedReader
BufferedReader works in conjunction with FileReader
Acts as a wrapper to FileReader
Delivers larger chunks to Java and make the process more efficient
Can define the size of the buffer using constructor (example of 8kB buffer)
PrintWriter & FileWriter
The PrintWriter class enables you to write formatted data to an
underlying Writer.
- underlying writer example is FileWriter
The FileWriter class makes it possible to write a file as a stream of
characters.
Lets see how save works
We need to create a file to save to : x.csv
//How do we save the data to a file called x.csv
private final static String LOG_FILE = "x.csv";
We start off by creating a String called LOG_FILE and making it final static
means no changes can ever be made to it and it is final
How do we call the save()
method
Using an actionListener
if( actionObject.equals( jbSave ) ){
save();
}
What does the save() method
do?
private void save(){
// Open the file with Append
PrintWriter output = new PrintWriter(new FileWriter(LOG_FILE,true));
output.printf("%s,%s%n", jtfField1.getText(), jtfField2.getText() );
output.close();
}
Here we see how PrintWriter and FileWriter are used in conjunction
How do we load and display?
Start with a handler that tells us which record is active in a way, or current
private int displayNum= 0;
Handler
// Which record to display to user.
When data is read from a file it is read one line at a time
So we need to store it in an array.
private ArrayList<String> logFileData = new ArrayList<String>();
/**We use an arraylist when we want something dynamic with no upper
bound. Array is bounded to the size at which it is initialized In this example
we will read a new line and add it to an arraylist as an element.**/
How do we trigger the loading?
We use an actionListener which calls a particular method
loadFile() is our method and we pass the x.csv file to it and the arraylist we just
created in the previous slide
Something is returned by loadFile() called numLoaded
What does the loadFile() method
do?
We want to know how many records or lines were loaded
If nothing is read then numLoaded remains as -1 (error) or it is equal to
arraylist size
Lets look at what the display
method does
What about Next and Previous
Records
displayNum is used as a
handler.
Remember that the
loadFile() method sets
displayNum variable to 0
at the start
We first call the
getPrevious() method
using an action listener
The display() function is
called each time to parse
the corresponding index
of arraylist
Then split it with ,
delimeter
Lets look at what the display
method does - again
String Formatting
pi = 22/7;
String y = String.format( "%.4f", pi );