/* Program to reverse a .dat sound file. */ import java.io.*; import java.util.StringTokenizer; public class Reverse { public static void main(String[]args) { if (args.length != 2) { System.err.println(" Incorrect number of arguments"); System.err.println(" Usage: "); System.err. println("\tjava Reverse "); System.exit(1); } try { // // Set up the input file to be read, and the output file to be written // BufferedReader fileIn = new BufferedReader(new FileReader(args[0])); PrintWriter fileOut = new PrintWriter(new BufferedWriter(new FileWriter(args[1]))); // // Read the first line of the .dat file. We want to store the // "sample rate" in a variable, but we can ignore the rest // of the line. We step through the first line one token (word) // at a time using the StringTokenizer. The fourth token // is the one we want (the sample rate). // StringTokenizer str; String oneLine; int sampleRate; String strJunk; oneLine = fileIn.readLine(); str = new StringTokenizer(oneLine); strJunk = str.nextToken(); // Read in semicolon strJunk = str.nextToken(); // Read in "Sample" strJunk = str.nextToken(); // Read in "Rate" sampleRate = Integer.parseInt(str.nextToken()); // Read in sample rate // // Read in the file and place values from the second column // in the stack. The first column values are thrown away. // We stop reading if we reach the end of the file. // DoubleStack s = new DoubleStack(); String timestep; double data; while ((oneLine = fileIn.readLine()) != null) { str = new StringTokenizer(oneLine); timestep = str.nextToken(); // Read in time step value from first column data = Double.parseDouble(str.nextToken()); // Read in data value from second column s.push(data); } // // Now we are ready to output the data values to output file. // But first, we need to output the header line // "; Sample Rate " // fileOut.println("; Sample Rate " + sampleRate); // Since the first column consists of numbers which start // at 0 and increase by 1/sampleRate every time slice, we'll // just use numSteps to recalculate these numbers. int numSteps = 0; // Finally, we print the values in reverse order (by popping // them off the stack). The first column consists of numbers // which start at 0 and increase by 1/sampleRate per row, so // we'll use numSteps/sampleRate to recalculate the appropriate // values. Uniform spacing will be accomplished by printing a tab. while (!s.isEmpty()) { fileOut.println((double) numSteps / sampleRate + "\t" + s.top()); s.pop(); numSteps++; } // // Close the files // fileIn.close(); fileOut.close(); } catch(IOException ioe) { System.err. println ("Error opening/reading/writing input or output file."); System.exit(1); } catch(NumberFormatException nfe) { System.err.println("Error in file format"); System.exit(1); } } }