Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.text.*;
  4.  
  5. public class Lab9 {
  6.     private final String FILEDIRECTORY = "C:\\Users\\Owner\\Desktop";
  7.     private final String INPUTFILEPATH = "Lab9Input.txt";
  8.     private final String OUTPUTFILEPATH = "Lab9Output.txt";
  9.    
  10.     public static void main(String[] args){
  11.         Lab9 myClass = new Lab9();
  12.         myClass.doIt();
  13.     }
  14.    
  15.     public void doIt(){
  16.         int recordCounter = 1;
  17.         double payAmount = 0;
  18.         DecimalFormat formatPay = new DecimalFormat("#####.##");
  19.         String employeeName = null;
  20.         String lineIn = null;
  21.         String[] piecesOfDocument = new String[2];
  22.         String myInputFile = FILEDIRECTORY + File.separator + INPUTFILEPATH;
  23.         String myOutputFile = FILEDIRECTORY + File.separator + OUTPUTFILEPATH;
  24.        
  25.         try{
  26.             File inputFile = new File(myInputFile);
  27.             FileReader fr = new FileReader(inputFile);
  28.             BufferedReader br = new BufferedReader(fr);
  29.             File outputFile = new File(myOutputFile);
  30.             FileWriter fw = new FileWriter(outputFile);
  31.             BufferedWriter bw = new BufferedWriter(fw);
  32.            
  33.             while ((lineIn = br.readLine()) != null){
  34.                 piecesOfDocument = lineIn.split(":");
  35.                 employeeName = piecesOfDocument[0];
  36.                 payAmount = Double.parseDouble(piecesOfDocument[1]);
  37.                 bw.write(String.format("%4d %-20s %s %n", recordCounter, employeeName, formatPay.format(payAmount)));
  38.                 recordCounter++;
  39.             }
  40.        
  41.         br.close();
  42.         fr.close();
  43.         bw.close();
  44.         fw.close();
  45.        
  46.         }
  47.         catch (Exception ex){
  48.             MyError("File could not be read or written!", ex);
  49.         }
  50.  
  51.     }
  52.    
  53.     public void MyError(String errMsg, Exception ex){
  54.         System.out.println(errMsg);
  55.         ex.printStackTrace();
  56.         System.exit(1);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement