Here is a sample how to use random access files:
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
Option Explicit
'
' demo for random file access
'
#Import java#io#RandomAccessFile
Public Sub Command1_Click()
Dim fileName As String = "myFile.bin"
Const recLen = 80 ' common record length
Dim nRec As Integer
Dim s1 As String = "Good morning! I am String 1 with äöüÄÖÜß"
Dim s2 As String = "Hi! I am String 2"
Dim s3 As String = "Hello! I am String 3"
Dim s As String
Dim rf As New RandomAccessFile(fileName, "rws")
' fill our file with three strings
Call rf.writeUTF(fixedLen(s1, recLen))
Call rf.writeUTF(fixedLen(s2, recLen))
Call rf.writeUTF(fixedLen(s3, recLen))
' loop through file to see if strings are still there
For nRec = 1 To 3
Call rf.seek((nRec - 1) * (2 + recLen))
s = rf.readUTF()
Debug.Print nRec & ": " & s
Debug.Print "FP=" & rf.getFilePointer()
Next nRec
Call rf.close()
End Sub
Private Function fixedLen(s As String, sLen As Integer) As String
Dim t As String
Dim c As String
Dim i As Integer
' clean all chars out of ASCII 30 .. 127 to prevent
' variable length string encoding (UTF-8)
t = ""
For i = 1 To Len(s)
c = Mid(s, i, 1)
If (Asc(c) >= 32) And (Asc(c) <= 127) Then
t = t & c
Else
t = t & "?"
End If
Next i
If Len(t) > sLen Then
fixedLen = Left(t, sLen)
Else
fixedLen = t & Space(sLen - Len(t))
End If
End Function
|
Random access files are seldomly used in java, because the byte-layout of objects is undefined and not known to the developer.
Jabaco currently does not support passing byte arrays as parameter to function calls.
Hopefully, the sample will inspire you rather than frightening you.
Greetings
A1880