The values of data structures in a program are lost when the program terminates. If you want to keep the data after the program terminates, the data need to be stored in a file or database JAVA program can read a data and write a data from and to a file. This process is called File Input/Output (I/O) 

Steps to write a program using file input & output
  • Open a I/O stream objects (using FileReader or FileWriter class) 
  • Read/write a data to a I/O stream objects (using read() or write() methods) 
  • Close the I/O stream objects (using close() method)

 * 1) Read data from file named Revenue.txt.
    2) Write the information into FirstHalfYear.txt that contain company name and revenues for the
         first 6 months
    3) Calculate income tax amount
    4) Write the information into IncomeTax.txt consists of company name and income tax for every

import java.util.StringTokenizer;
public class RevenueApp
   public static void main(String args[])
        ///step 1
        //open Revenue.txt - read
        FileReader fr = new FileReader("Revenue.txt");
        BufferedReader br = new BufferedReader(fr);

        //open FirstHalfYear.txt - write
        FileWriter fw = new FileWriter("FirstHalfYear.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);

        //open IncomeTax.txt - write
        FileWriter fw2 = new FileWriter("IncomeTax.txt");
        BufferedWriter bw2 = new BufferedWriter(fw2);
        PrintWriter pw2 = new PrintWriter(bw2);

        //Print title into firstHalfyear.txt
        pw.println("COMPANY \t\t MONTHLY REVENUE");
        pw.println("\t\t jan \t\t Feb \t\t March \t\t April \t\t May \t\t June");

        //print title into incometax.txt
        pw2.println("2010 INCOME TAX");
        pw2.println("COMPANY \t\t TOTAL REVENUE \t\t INCOME TAX");

        ////step 2
        //read data line by line
        String input=null, insert, compName=null;
        long rev [] = new long[12];
        long totRev = 0;
        double incometax=0.0;
        while((input=br.readLine()) != null)
            StringTokenizer st = new StringTokenizer(input, ";");
                //read company name
                compName = st.nextToken();
                //read revenue for january;
                for(int i=0;i<rev.length; i++)
                    insert  = st.nextToken();
                    rev[i] = Long.parseLong(insert);

                    //calculate total revenue
                    totRev += rev[i];
             //c) Determine the subsidiary company's income tax amount based on the tax rate
             //calculate incometax amount
                incometax = totRev*0.15;
             else if(totRev>=100000 && totRev<=500000)
                incometax = totRev*0.25;
             else if(totRev>500000)
                incometax = totRev*0.35;
             //write the information into FirstHalfYear.txt
             pw.println(compName+"\t\t"+ rev[0] + "\t\t" + rev[1]+"\t\t"+ rev[2] + "\t\t" + rev[3]+"\t\t"+ rev[4] + "\t\t" + rev[5]);

             //write the information into Incometax.txt
             pw2.println(compName+"\t\t"+ totRev +"\t\t" + incometax);


        catch(FileNotFoundException fnfe)
        catch(IOException ioe)

No comments: