BinaryWriter.frink

Download or view 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[]


Download or view 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 20145 days, 6 hours, 7 minutes ago.