Home > Language Reference > Classes > StreamFile

Open Method

Creates and/or opens a file.

Syntax

Public Sub Open(ByVal iVolRef As Integer, ByRef sName As String, ByVal eMode As HbMode, Optional ByVal iBufferSize As Integer = 512)

ParameterDescription
iVolRef Volume reference.
sName File name.
eMode Open mode.
iBufferSizeInternal buffer size.

Remarks

The Open method opens or creates a file located on an expansion card and associates it with this instance of the StreamFile class. All subsequent operations carried out using the properties and methods of this object apply to this file.

The iVolRef parameter specifies the card to look for the file on. The only valid values for this parameter are the volume references returned by the Reference property of the VFSVolume object. Refer to the description of this object or the example below for further information.

The sName parameter specifies the name of the file to open and should include the full file path. The root directory is designated "/". It is not possible to open a file that has the hbFileAttrDirectory attribute with this method.

The eMode parameter specifies the mode to use when opening a file and can be a combination of the constants from the following groups:

To indicate the file access mode, specify one of the following constants:

ConstantValueDescription
hbModeReadOnly&H0001Read only access.
hbModeWrite&H0002Write only access.
hbModeReadWrite&H0003Read and write access.

To indicate the file creation mode, specify one of the following constants:

ConstantValueDescription
hbModeOpenExisting&H0000Open an existing file. If it does not exist, an error occurs.
hbModeOpenAlways&H1000Open an existing file. If it does not exist, it is created.
hbModeCreateAlways&H2000Create a file. If it already exists, overwrite it.

Optionally, you can also specify any of the following constants:

ConstantValueDescription
hbModeExclusive&H0008Exclude anyone else from using the file until it is closed.
hbModeLeaveOpen&H0004Leave the file open even after the application exits.

If you specify the creation mode hbModeOpenAlways or hbModeCreateAlways, you should also specify the access mode hbModeWrite or hbModeReadWrite. In effect, these file open modes require the creation or truncation of the file which is why they must be available for writing.

Finally, the optional iBufferSize parameter impacts the way file operations are cached. If it is set to zero, no buffering is used, and every file access is passed "as is" to the underlying operating system. This mode is discouraged for normal operation. On the other hand, if it is set to any value between 128 and 32767 inclusives, it specifies the size of the internal buffer.

The default buffer size is 512 bytes. This should be suitable for most applications, but it may be necessary to tune this value in some circumstances. Notably, if your application writes large chunks of data, such as a whole database, performance may be improved by increasing the buffer size.

The following example creates the "tmp.txt" file in the root directory of the first volume found, and writes some text:

Private Sub Button1_Click()
  Dim v as New VFSVolume
  Dim s as New StreamFile
  
  v.FindFirstVolume
  s.Open v.Reference, "/tmp.txt", hbModeWrite+hbModeCreateAlways
  s.Write "Hello World !", hbWriteNoNull
  s.Close
End Sub

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0Requires an expansion slot.