import java.io.*; /** * A short example of how to use some of the functions in BinaryFile. First * creates a binary file that contains various types, and then rereads those * same types. * * @author Jeff Heaton(http://www.jeffheaton.com) * @version 1.0 */ class BinaryExample { /** * The main function. Used to run the test. * * @param args * Not really used, but required by Java. * @exception java.io.FileNotFoundException */ public static void main(String args[]) throws FileNotFoundException { int i; String stra, strb, strc, strd; RandomAccessFile file; BinaryFile bin; // set the endian mode to run the test in final short endian = BinaryFile.BIG_ENDIAN; // set the signed mode to run the test in final boolean signed = true; try { file = new RandomAccessFile("./test.dat", "rw"); bin = new BinaryFile(file); bin.setEndian(endian); bin.setSigned(signed); bin.writeFixedString("Fixed String", 80); bin.writeFixedZeroString("Fixed zero string", 80); bin.writeLengthPrefixString("Pascal String"); bin.writeZeroString("Zero String"); bin.writeByte((short) 100); bin.writeWord(1000); bin.writeDWord(100000); file.close(); file = new RandomAccessFile("test.dat", "r"); bin = new BinaryFile(file); bin.setEndian(endian); bin.setSigned(signed); stra = bin.readFixedString(80); strb = bin.readFixedZeroString(80); strc = bin.readLengthPrefixString(); strd = bin.readZeroString(); short b = bin.readByte(); int w = bin.readWord(); long dw = bin.readDWord(); file.close(); System.out.println("Str a = " + stra); System.out.println("Str b = " + strb); System.out.println("Str c = " + strc); System.out.println("Str d = " + strd); System.out.println("**B=" + b); System.out.println("**W=" + w); System.out.println("**DW=" + dw); } catch (Exception e) { System.out.println("**Error: " + e.getMessage()); } } }