// CS1501 -- Summer 2006 // Intro to using binary files in Java for input and output. This program will // simply copy any file, whether it is text, executable or anything else. // It will then output the 8 leftmost bits and the 8 rightmost bits of an // integer to demonstrate some binary operations. Finally, it will write // some odd-length values to the file and read them back using buffers. import java.io.*; import javax.swing.*; public class binaryio { public static void main(String [] args) { FileInputStream infile = null; FileOutputStream outfile = null; String ifname = null; // string for name of input file String ofname = null; // string for name of output file int ch; while (true) { try { ifname = JOptionPane.showInputDialog(null, "File name?"); infile = new FileInputStream(ifname); break; } catch (IOException e) { System.out.println("File " + ifname + " could not be opened"); } } ofname = ifname + ".cop"; try { outfile = new FileOutputStream(ofname); ch = infile.read(); // Read ahead because we must test for end of file while (ch != -1) { outfile.write(ch); ch = infile.read(); // get the next byte } infile.close(); outfile.close(); } catch (IOException e1) { System.out.println("Some bogusness with the file"); System.exit(0); } FileOutputStream newFile = null; try { newFile = new FileOutputStream("wacky.out"); int val = 1090519178; // 2^30 + 2^24 + 2^7 + 2^3 + 2^1 int bits = 8; int leftout = 0; int rightout = 0; leftout = val >> (32-bits); // should be 01000001 = 65 rightout = (val << (32 - bits)) >> (32 - bits); // should be 10001010 = 138 newFile.write(leftout); // write a byte newFile.write(rightout); // write a byte long buffer = 0L; long ander = (1L << 32) - 1; // leftmost 32 bits are 0 // rightmost 32 bits are 1 int bitsUsed = 0; // The codes below require the number of bits shown afterward int [] codes = {8, 17, 3, 65, 63, 128}; int [] numb = {4, 5, 2, 7, 6, 8}; for (int i = 0; i < codes.length; i++) { // Trace this on paper to see what it does buffer = buffer | (codes[i] << (32 - bitsUsed - numb[i])); buffer = buffer & ander; // Zeroify left 32 bits -- this is // done because Java does not have the unsigned int // type bitsUsed += numb[i]; } while (bitsUsed > 0) { newFile.write((int) (buffer >> 24)); // write bits 33-40 // (leftmost bits of 32 bit int) buffer = buffer << 8; // shift 8 bits left buffer = buffer & ander; // zeroify left 32 bits bitsUsed -= 8; } newFile.close(); FileInputStream reFile = new FileInputStream("wacky.out"); int lval = reFile.read(); int rval = reFile.read(); System.out.println("leftmost " + bits + " bits = " + lval); System.out.println("rightmost " + bits + " bits = " + rval); buffer = 0L; bitsUsed = 0; for (int i = 0; i < 4; i++) // read next 4 bytes { rval = reFile.read(); // read a byte // Trace to see what code below does buffer = buffer | (rval << (32 - bitsUsed - 8)); buffer = buffer & ander; // zeroify left 32 bits bitsUsed += 8; } for (int i = 0; i < numb.length; i++) { lval = (int) (buffer >> (32 - numb[i])); buffer = buffer << numb[i]; buffer = buffer & ander; // zeroify left 32 bits System.out.println("Next value is " + lval); } reFile.close(); } catch (Exception e) { System.out.println("Some exception " + e + " has occurred"); } } }