Android Question RandomAccessFile

msfactory

Member
Licensed User
Longtime User
I'm sorry not good at English.
I, I tried RandomAccessFile the code below, but it becomes the value of all 0 Looking at the log "Value2".
Please let me know if there is some solution
Thank you very much in advance.





Sub Process_Globals
Dim value1(1000) As Int
Dim value2(1000) As Int

End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
'------------------READ-------------------------------------------
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal, "1.dat", False)

For i = 0 To 999
value1(i) = i
Next

For i = 0 To 999
raf.WriteInt(value1(i),i)
Next
raf.Close
'-----------------------------------------------------------------


'------------------WRITE-----------------------------------------------
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal, "1.dat", False)

For i = 0 To 999
value2(i) = raf.ReadInt(i)
Next

raf.Close
'------------------------------------------------------------------

For i = 0 To 999
Log(value2(i))
Next

End Sub
 

msfactory

Member
Licensed User
Longtime User
B4X:
Sub Process_Globals

   Dim value1(1000) As Int
   Dim value2(1000) As Int
  

End Sub

Sub Globals


End Sub

Sub Activity_Create(FirstTime As Boolean)

   '------------------READ-------------------------------------------
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "1.dat", False)  
  
   For i = 0 To 999
    value1(i) = i
   Next
  
   For i = 0 To 999
    raf.WriteInt(value1(i),i)
   Next

   raf.Close
   '-----------------------------------------------------------------
  
  
   '------------------WRITE-----------------------------------------------
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "1.dat", False)
  
   For i = 0 To 999
    value2(i) = raf.ReadInt(i)
   Next
  
   raf.Close
   '------------------------------------------------------------------
  For i = 0 To 999
   Log(value2(i))
  Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
I'm sorry
Code is this.
Thank you very much in advance.
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
The RAF library is byte oriented. Is like a VB6 binary file. Your code should be something like this:

B4X:
Sub Process_Globals
   Dim value1(1000) As Int
   Dim value2(1000) As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
'------------------READ-------------------------------------------
Dim raf As RandomAccessFile
    raf.Initialize(File.DirInternal, "1.dat", False)
    For i = 0 To 999
        value1(i) = i
    Next

     For i = 0 To 999
         raf.WriteInt(value1(i),raf.currentposition) 
         'Raf.currentposition=0 before the first int is written, then is automatically set
         ' at 2 for the 2nd int, 4 for the 3rd and so on (I'm assuming the int takes 2 bytes)
     Next
     raf.Close
'-----------------------------------------------------------------


'------------------WRITE-----------------------------------------------
Dim raf As RandomAccessFile
     raf.Initialize(File.DirInternal, "1.dat", False)
      Value2(0) = raf.readint(0)  'read the 1st int at the beginning of the file
    For i = 1 To 999 '1 not zero. The first already written

        value2(i) = raf.ReadInt(raf.currentposition)
    Next

    raf.Close
'------------------------------------------------------------------

    For i = 0 To 999
        Log(value2(i))
    Next

End Sub

Readint(x) does not read the int number x. It reads the integer which is written at the offset x in the file (offset is from zero to the number of bytes in the file -1)

If you know the binary files in basic... It's the same.
 
Upvote 0

msfactory

Member
Licensed User
Longtime User
Hi Straker

Thank you for the quick reply
When I tried the code that you taught me, working properly
Thank you very much!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…