/* Written by: Jen Chen Date: Summer 2005 Description: An example to show how to read into the contents of a file, then write the desiring result to another file (in this case we write the output to an html file). Note: (1) In order to read into the contents of a file, we need to create an object of the class BufferedReader, which will wrap around an object of the class FileReader(file_name); where the parameter file_name is the name of the file that we want to read from. ex: BufferedReader buf = new BufferedReader(new FileReader("Details.csv"); the above statement creates an object, called "buf", of the class BufferedReader. This object stores the contents of the file Details.csv. To read the contents of the "buf" object, we call the method readLine() from the class BufferedReader(). This method will read into the contents of the Details.csv file, one line at a time, until it encounters the EOF signal (or null). Since most of the files have more than one lines, then we need to loop through the entire contents of the file; ie we need to create a while-loop to do this. This while-loop will check to see whether the readLine() method returns a null or not. If the readLine() method returns a null, then we reach the end of the file; thus our program would exit the while-loop. Otherwise our while-loop will keep looping until it reaches the EOF (which the method readLine() would return null). Note that the method readLine() will return a string which contains the contents of the entire line (or record) from the file. If the field in this file is seperated by a comma (in this case, since it is a csv file), then to extract the elements of each line (record), we need to call the method split() from the String class. The split() method will return an array of string; where the elements of this array are the fields from each line (record). For example, the first line of the Details.csv file is: Order ID,Product,Unit Price,Quantity,Discount Then the readLine() method would contain the String "Order ID,Product,Unit Price,Quantity,Discount" To extract these elements, we need to "split()" these elements from the the above String by invoking the split(",") method with the "," delimiter supplied as a parameter to the split() method. Remember that the split() method returns an array of String; therefore we need to create an array of String to store the resulting String array returned by the split() method. Make sure that this is applicable ONLY IF the number of fields in each record remains contants throughout the entire file. For records with different number of fields, then we would deal with differently. The main idea is that we would manipulate the fields of each record inside the while-loop. (look at example below) (2) To write to a file, we need to create an object of the class BufferedWriter, which wraps around an object of the class FileWriter("output_file_name"); where output_file_name is the name of your output file. ex: BufferedWriter bw = new BufferedWriter(new FileWriter("output.html")); To write to the output.html file, we'd invoke the write() method from the BufferedWriter class. (look at the example below). Finally, DO NOT forget to close() BOTH the objects buf and bw as follow: buf.close(); bw.close(); Without closing the bw object, then whatever we write() to that object will not be written to the output file. */ import java.io.*; public class File_IO1Summer05 { public static void main(String[] args) throws IOException{ String file = null, line; file = "Details.csv"; //file to read into. BufferedReader buf = null; BufferedWriter bw = new BufferedWriter(new FileWriter("output.html")); buf = new BufferedReader(new FileReader(file)); line = buf.readLine(); //Read into the contents of the first line (record). line = buf.readLine(); //Read into the contents of the 2nd line (record); therefore we overwrite the contents of the first line. //The reason for this is to skip the 1st record, which contains the column header. String[] st = new String[5]; //Since each line from the Details.csv file has 5 elements, then we //create a String array with 5 elements to store the elements from the String "line". double amt = 0.0, sum = 0.0; bw.write("jen Chen: Lesson 1"); //Write the html header to the output.html file. bw.write("

"); bw.write("
"); bw.write(""); //Now we loop through the entire contents of the Details.csv file. while(line != null){ //while there is still something to read from the Details.csv file, then keep reading into that file. //We store the elements of the line (record) into the String array st. st = line.split(","); //Make sure that we specify the correct field delimiter in the split() method. amt = Double.parseDouble(st[2]) * Integer.parseInt(st[3]); //Convert the field UnitPrice into a double type. amt = Math.round(amt*100)/100; //calculate the amount of each item (record). sum += amt; //calculate the grand total. //Now we output the result to a row of the output.html file. bw.write(""); line = buf.readLine(); //read into the next line. }//end of WHILE() //Now create another row to display the grand total of all items. bw.write(""); bw.write("
" + st[0] + "" + st[1] + "$" + st[2]); bw.write("" + st[3] + "$" + Double.toString(amt) + "
   GRAND TOTAL:$" + sum + "
"); bw.write("
"); bw.write(""); bw.close(); //close the BufferedWriter. buf.close(); //close the BufferedReader.}//end of main() }//end of class File_IO1{}