//******************************************************************** // The purpose of this code is to demonstrate binary operations. // This code will read and count the number of bytes in a file input by the user, // then push that integer value onto the first four bytes of an output file, // then read back in the first four bytes of that output file, and put them back // into an integer value and print it out. By understanding this code and // running it, you can see how binary files are read from, written to, // and you can see how to shift bytes one at a time into an integer. // You will also learn how to count the number of bytes in a file. // (Adapted from a Fall 2006, 1501 student assignment) //******************************************************************** import java.io.*; public class ByteCountDemo { private String originalfile; //the file whose size will be counted //******************************************************************* // Name: ByteCountDemo(constructor) // Description: Instantiates the ByteCountDemo object. //******************************************************************* ByteCountDemo(String fname) { originalfile = fname; } //******************************************************************* // Name: countBytesAndWrite // Description: Counts the number of bytes in originalfile and // outputs that value to a binary file specified by the input parameter // (assuming it is not too big) //******************************************************************* public void countBytesAndWrite(String outfilename) { FileInputStream infile = null; FileOutputStream outfile = null; try { //Opens the stream infile = new FileInputStream(originalfile); int counter=0; //count the number of bytes in the file while (infile.read() != -1) //-1 is the end of file sentinel counter++; infile.close(); outfile = new FileOutputStream(outfilename); //writes the counter value as the first 4 bytes of the output file //(if the size is greater than 2^32 bits (around 4 gigs) then the decoding wont work right) outfile.write(counter>>>24); outfile.write(counter>>16); outfile.write(counter>>8); outfile.write(counter); outfile.close(); } catch(FileNotFoundException e) { System.out.println("File Not Found: " + originalfile); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); } } //******************************************************************* // Name: readFourBytes // Description: Reads the first four bytes from the file passed in // through the parameter, shifts them into one integer, and returns // out the value of that integer. //******************************************************************* public int readFourBytes(String thefilename) { FileInputStream infile = null; int size=0; //will hold the value of the first four bytes of the file try { //Opens the file, notice this program adds the .enc extension //to the input file. Therefore the original file must be //the passed filename. infile = new FileInputStream(thefilename); //reads the first 4 bytes and shifts them into one integer variable size = infile.read(); size = ((size << 8) | infile.read()); size = ((size << 8) | infile.read()); size = ((size << 8) | infile.read()); infile.close(); return size; } catch (IOException e) { System.err.println("IOException " + e.getMessage()); } return 0; } public static void main (String[] args) { System.out.println("Enter the name of the file whose size in bytes you would like me to count:"); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); try { String filename = keyboard.readLine(); ByteCountDemo bcd = new ByteCountDemo(filename); //we write four bytes (that represent the file size) to this output file bcd.countBytesAndWrite("binaryoutput.txt"); int filesize = bcd.readFourBytes("binaryoutput.txt"); //then we read them right back in System.out.println("The file you input is " + filesize + " bytes."); } catch (Exception e) { System.out.println(e); } } }