/** * CheckFile: Check to see if a file is sorted. * This assumes that each record is a pair of short ints * with the first short being the key value * @author CS3114 Instructor and TAs * @version 10/18/2014 */ import java.io.*; public class CheckFile { public static void main(String args[]) throws IOException { if (args.length != 1) { System.out.println("Usage: CheckFile "); return; } DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(args[0]))); if ((in.available() < 4) || ((in.available() % 4) != 0)) { System.out.println("File size of " + in.available() + " bytes indicates a bad file"); return; } // Prime with the first record short key2 = in.readShort(); short value2 = in.readShort(); int reccnt = 0; try { while (true) { short key1 = key2; short value1 = value2; key2 = in.readShort(); value2 = in.readShort(); reccnt++; if(key1 > key2) { System.out.println("Key " + key1 + " at position " + (reccnt - 1) + " is out of order with key " + key2 + " at position " + reccnt); } } } catch (EOFException e) { System.out.println(reccnt + " records processed"); } } }