BinaryWriter.frink

View or download BinaryWriter.frink in plain text format


/* This class demonstrates reading and writing binary data to/from a file.
   It simply creates a java.io.DataOutputStream and writes using its methods.
*/


openBinaryWriter[path, append=false] :=
{
   os  = newJava["java.io.FileOutputStream", [path, append]]
   dos = newJava["java.io.DataOutputStream", [os]]
   return dos
}

openBinaryReader[path] :=
{
   istr  = newJava["java.io.FileInputStream", [path]]
   dis = newJava["java.io.DataInputStream", [istr]]
   return dis
}

// Write a binary file.
filename = "data.txt"
w = openBinaryWriter[filename]
w.writeBoolean[true]
w.writeByte[0xFF]
w.writeInt[123]
w.writeDouble[99.9]
w.close[]

// Now read it in.
r = openBinaryReader[filename]
println["Read " + r.readBoolean[]]
println["Read " + r.readUnsignedByte[]]
println["Read " + r.readInt[]]
println["Read " + r.readDouble[]]
r.close[]


View or download BinaryWriter.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17592 days, 15 hours, 31 minutes ago.