Advertisement
Vita_Harvey

UnBackMasker_D

Feb 18th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package csc143.backmask;
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6. import csc143.data_structures.*;
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PA6
  11.  * A class which reads the textual input from a DAT file,
  12.  * reverses it, and thereby produces a "backmasked" or "unbackmasked" audio file.
  13.  */
  14. public class UnBackMasker {
  15.  
  16.    // Fields:
  17.    // Create new input file, read from input file using Scanner.io or something
  18.    // Declare only, take out initialization
  19.    private File inFile;
  20.    //PrintWriter or PrintStream for output file (written to)
  21.    private File outFile;
  22.    
  23.    /**
  24.     * Class constructor.
  25.     * Will do the "work" of reversing the appropriate info from the source
  26.     * file and writing it to the output file.
  27.     * Returns a "backwards" version of the .dat file we originally input.
  28.     * @param File inFile, the file to be opened and read from.
  29.     * @param File outFile, the file to be created and written to from input.
  30.     * @return A new .dat file with the displacement info written in opposite order
  31.     */
  32.    public UnBackMasker(File inFile, File outFile)
  33.       throws IOException {
  34.    
  35.       // Get input file (to be read from)
  36.       inFile = new File("" + inFile + "");
  37.       FileReader fr=new FileReader(inFile);
  38.       BufferedReader br = new BufferedReader(fr);
  39.       Scanner scan = new Scanner(inFile);
  40.      
  41.       // Open input file
  42.       // Create output file (to be written to)
  43.       outFile = new File("" + outFile + "");
  44.       PrintWriter output = new PrintWriter(outFile);
  45.      
  46.       // Write headers from inFile to outFile directly
  47.       FileWriter fw = new FileWriter(outFile);
  48.       BufferedWriter bw = new BufferedWriter(fw);
  49.      
  50.       // Reads first header line.
  51.       String header1 = new String(br.readLine());
  52.       // Reads second header line.
  53.       String header2 = new String(br.readLine());
  54.       br.close();
  55.       // We write each header line on its own line in outFile.
  56.       bw.write(header1);
  57.       bw.newLine();
  58.       bw.write(header2);
  59.       bw.newLine();
  60.       bw.flush();
  61.       bw.close();
  62.      
  63.       // Create an UnboundedArrayQueue to keep the content of time column.
  64.       UnboundedArrayQueue timeQ = new UnboundedArrayQueue(100);
  65.       // Create an UnboundedArrayStack to keep the content of displacement column.
  66.       UnboundedArrayStack dispStack = new UnboundedArrayStack(100);
  67.      
  68.       // lines is the number of elements stored in each data structures.
  69.       int lines = 0;
  70.      
  71.       // Puts data from input file into data structures
  72.       while(scan.hasNextLine()) {
  73.          String time = scan.next();
  74.          String disp = scan.next();
  75.          timeQ.add(time);
  76.          dispStack.push(disp);
  77.          lines++;
  78.          scan.nextLine();                          
  79.       }
  80.      
  81.       // Print the appropriate data from our structures into output file
  82.       while(lines > 0) {
  83.          // Write left-aligned, 13-char-wide string of first thing added
  84.          // to timeQ queue.
  85.          output.printf("%-13s", timeQ.peek());
  86.          // Take off that item so peek refers to next one next time around.
  87.          timeQ.remove();
  88.          // Write right-aligned, 12-char-wide string of last thing added
  89.          // to dispStack stack.
  90.          output.printf("%13s", dispStack.peek());
  91.          dispStack.pop();
  92.          // Advance to the next line of the outFile.
  93.          output.println();
  94.          lines--;                    
  95.       }      
  96.       // Flush resources, close input file (don't forget)
  97.       output.flush();
  98.       output.close();        
  99.    }
  100.    
  101.    /**
  102.     * Will accept user input into args array,
  103.     * use these Strings to specify an input file to read from
  104.     * and an output file to write to, then uses these
  105.     * files as arguments "in" and "out" to the class
  106.     * constructor's respective "inFile" and "outFile" params.
  107.     */
  108.    public static void main(String[] args)
  109.       throws IllegalArgumentException {
  110.    
  111.       File in;
  112.       File out;
  113.      
  114.       // Main report errors
  115.       if (args.length != 2) {
  116.          throw new IllegalArgumentException("Only accepts two arguments.");
  117.       }else{
  118.          in = new File(args[0]);
  119.          out = new File(args[1]);
  120.          try {                      
  121.             new UnBackMasker(in, out);
  122.             System.out.println("File backmasking successful.");
  123.             return;
  124.          }catch(IOException e){
  125.            
  126.             System.err.println("Useage: UnBackMasker " + in + " " + out
  127.                + " " + e.getMessage());
  128.             return;
  129.             }
  130.       }    
  131.    }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement