. .


ST 4U 291: Reading and Writing Binary Files in VA Smalltalk

October 3, 2012 8:25:44.990

Today's Smalltalk 4 You goes through a simple example - reading and writing a file in binary mode in VA Smalltalk. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Binary Data.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll go through a small example - how to write, and read, a simple binary file. To start, let's write a couple of numbers to a file:

stream := FileStream write: 'testBin.bin'.
stream isBytes: true.
stream nextPut: 2.
stream nextPut: 3.
stream close.

Even though we are using the abstract class FileStream, VA picks up the correct concrete subclass when we use the #write: method. Next, we'll want to read the file in binary mode (again, using #isBytes:)

stream := FileStream read: 'testBin.bin'.
stream isBytes: true.
first := stream next.
second := stream next.
stream close.
^Array with: first with: second.

Inspect the results to ensure that you got back what you expect - you should see something like this:

Binary Data

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

[st4u291-iPhone.m4v ( Size: 2138732 )]

posted by James Robertson

 Share Tweet This