/* Author : Michael Robinson Program : filesBasicClass.java Purpose : Create new text file & write data into file Read and process data from text file Open existing text file, write and read data into/from file Also converts String to double and String to integer To Create and write to a new file; FileWriter customers = new FileWriter("customers.txt"); PrintWriter output = new PrintWriter( customers ); To Open an existing text file and append data to it; FileWriter customers = new FileWriter("customers.txt", true); PrintWriter output = new PrintWriter( customers ); To open and read and existing text file: FileReader customers = new FileReader("customers.txt"); BufferedReader inputFile = new BufferedReader( customers ); Updated : May 4, 2099 */ import java.io.*; import java.util.Scanner; public class filesBasicClass { // To Create and write to a new file; public static void openWriteNewFile() throws IOException { System.out.println( "\n At 1 – openWriteNewFile()" + " creating new file and adding: " ); //customers.txt is the name of the file to be written //to the secondary storage (hard disk, CD, etc) FileWriter customers = new FileWriter( "customers.txt" ); //customers is the FileWriter class which has all the //the information about customers.txt the file to be //written to secondary storage (cd, hard disk etc) PrintWriter output = new PrintWriter( customers ); //write the numbers from 10 to 0 to the output file //and display them to the screen for( int x = 10; x >= 0; x-- ) { output.println( x ); System.out.print( " " + x ); } output.close(); //closes the file System.out.println( "<---eof" ); }//end public static void openWriteNewFile() throws IOException public static void openWriteExistingFile() throws IOException { System.out.print( "\n At 2 - openWriteExistingFile() " + "Appending data: " ); //customers is the FileWriter class which has all the //the information about the customers.txt file FileWriter customers = new FileWriter( "customers.txt", true ); //output is the PrintWriter Class that has all the //information about the customers object of the //FileWriter class PrintWriter output = new PrintWriter( customers ); //write the numbers from 10 to 15 to the output file //and display them to the screen for( int x = 10; x < 16; x++ ) { output.println( x ); System.out.print( " " + x ); } output.close(); System.out.println( "<---eof" ); }//end public static void openWriteExistingFile() //To open and read and existing text file: public static void openReadExistingFile() throws IOException { System.out.print( "\n At 3 - openReadExistingFile() : " ); //customers is the FileReader class which has all the //the information about customers.txt which is the file //to be read from secondary storage (cd, hard disk etc) FileReader customers = new FileReader( "customers.txt" ); //inputFile is the BufferedReader class which buffers the //data read by the customers object of the FileReader class BufferedReader inputFile = new BufferedReader( customers ); //to store data read from customers.txt text file String inputFileRecord; //reads one record from customers.txt text file inputFileRecord = inputFile.readLine(); //field to append all records read from customers.txt file String temp=""; //read all records found in customers.txt file until it finds //the null character indicating EOF (end of file) while( inputFileRecord != null) { temp = temp + inputFileRecord; //append records System.out.print( "[" + temp + "]" ); inputFileRecord = inputFile.readLine(); //read record } inputFile.close(); System.out.println( "<---eof" ); }//end public static void openReadExistingFile() public static void addAllValuesInFile() throws IOException { System.out.print( "\n At 4 - addAllValuesInFile() : " ); //customers is the FileReader class which has all the //the information about customers.txt which is the file //to be read from secondary storage (cd, hard disk etc) FileReader customers = new FileReader( "customers.txt" ); //inputFile is the BufferedReader class which buffers the //data read by the customers object of the FileReader class BufferedReader inputFile = new BufferedReader( customers ); //to store data read from customers.txt text file String inputFileRecord; //reads one record from customers.txt text file inputFileRecord = inputFile.readLine(); //to do calculations with the data from the above files double sumDouble = 0; int sumInteger = 0; //read all records found in customers.txt file until it finds //the null character indicating EOF (end of file) while( inputFileRecord != null ) { //converts String to Double sumDouble = sumDouble + Double.parseDouble(inputFileRecord); //converts String to int sumInteger = sumInteger + Integer.parseInt(inputFileRecord); System.out.print( " " + inputFileRecord ); inputFileRecord = inputFile.readLine(); //read record } inputFile.close(); System.out.printf( " = Total Double = %.2f Total Integer = %d", sumDouble, sumInteger); }//end public static void addAllValuesInFile() public static int menu() { System.out.print( "\n\t*********************************\n\t" + "* Enter file mode *\n\t" + "* 1 - openWriteNewFile *\n\t" + "* 2 - openWriteExistingFile *\n\t" + "* 3 - openReadExistingFile *\n\t" + "* 4 - addAllValuesInFile *\n\t"); System.out.print( "\n\t Enter 1, 2, 3, or 0 to exit: " ); //read the keyboard Scanner keyboard = new Scanner( System.in ); int fileType = keyboard.nextInt(); return( fileType); }//end public static int menu() public static void main( String arg[] ) throws IOException { System.out.println( "\n Working with files – basic" ); int fileType = menu(); while( fileType != 0 ) { switch( fileType ) { case 0 : break; case 1 : openWriteNewFile(); break; case 2 : openWriteExistingFile(); break; case 3 : openReadExistingFile(); break; case 4 : addAllValuesInFile(); break; default : System.out.println( "\tPlease Enter " + "1, 2, 3, or 0 to exit" ); } fileType = menu(); }//end while( fileType != 0 ) System.out.println( "\n\t End Of Program" ); }//end public static void main( String arg[] ) }//end public class filesBasicClass