Write a program that reads a text file called salestxt The f
Solution
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports for the code
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.StringTokenizer;
public class salesDataExample { // class to run the code
   
     public static void main(String[] args) throws IOException { // driver method that throws any exceptions
       
         File outFile = new File(\"orders.txt\"); // create a object for the output file
         if (!outFile.exists()) { // check for the existence and create one
             outFile.createNewFile();
         }
       
         FileWriter fw = new FileWriter(outFile.getAbsoluteFile()); // get the path of the file
         BufferedWriter bw = new BufferedWriter(fw); // create a writer object
       
         String currentLine; // local variables
         BufferedReader br = new BufferedReader(new FileReader(\"sales.txt\")); // reader object for the input file
         while ((currentLine = br.readLine()) != null) { // check for null
           
             StringTokenizer st = new StringTokenizer(currentLine, \" \"); // token the data read based on the delimiter
             String itemCode = st.nextToken(); // get the tokens saved
             int itemSold = Integer.parseInt(st.nextToken()); // conver the data to int
             int totalItems = Integer.parseInt(st.nextToken());
           
             int remItems = totalItems - itemSold; // check for the left over items
             int leastItems = (int) ((int) totalItems * 0.3); // calculate the percent of the total items
           
             if (remItems <= leastItems) { // check for the items count
               
                  bw.write(itemCode + \" \" + Integer.toString(totalItems - remItems)); // write the data to file the item code and the items needed to retain
                 bw.write(\"\ \"); // new line character
               
              }
         }
         bw.close(); // closing the reader and the writers
         br.close();
     }
 }
 sales.txt :
234 100 500
 856 280 280
 657 64 300
 741 2 600
 124 582 600
orders.txt :
856 280
 124 582
 Hope this is helpful.


