The RandomAccessFile is called just that. It's it own library. Most common IO functions are contained in the Core (default & mandatory) library.
For what you're after:
B4X:
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal,"1.dat",False)
raf.WriteInt(1,raf.CurrentPosition)
raf.WriteInt(2,raf.CurrentPosition)
raf.WriteInt(3,raf.CurrentPosition)
raf.Close
'Not required, just for demonstration
raf.Initialize(File.DirInternal,"1.dat",True)
Log(raf.ReadInt(0))
Log(raf.ReadInt(raf.CurrentPosition))
Log(raf.ReadInt(raf.CurrentPosition))
raf.Close
So, I'm writing an Int to this file but I don't see it anywhere. I thought I'd see it in the root but I didn't. Where does File.DirInternal get written to?
It's a folder private to your application. I don't think you can see it unless you are rooted. You can use File.Copy to move it to File.DirRootExternal, then you can see it.
Have a look here for definitions of where File.nnn refers to. File.DirDefaultExternal is the one to use if you want to be able to view the files manually.
I'm getting a FileNotFoundException with the following line of code. I'm trying to write data to this file, so I'm using False which is Writable. I assumed that if the file doesn't exits it will be created. So, I'm puzzled by the error.
Dim raf As RandomAccessFile
raf.Initialize(File.DirRootExternal,"cb2.dat",False) 'False means Writable, True means Read Only
raf.WriteInt(msb_Nightime,raf.CurrentPosition)
raf.Close
It stated working ok for me too so I'm not sure what was going on.
However, my code to write my Int values is not writing to the file. Via debugging my vars are set to the new values to be written to the file but when I open the file to read in the values (after saving), they are not what was supposed to be saved.
B4X:
Dim raf As RandomAccessFile
raf.Initialize(File.DirRootExternal,"cb2.dat",False)
raf.WriteInt(msb_Nightime,0)
raf.WriteInt(msb_Indoor,raf.CurrentPosition)
raf.WriteInt(msb_Outdoor,raf.CurrentPosition)
raf.Close
Does this code look right to write data to the file?
If File.Exists(File.DirRootExternal, "cb2.dat") = True Then
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal,"cb2.dat",True)
msb_Nightime = raf.ReadInt(0)
msb_Indoor = raf.ReadInt(raf.CurrentPosition)
msb_Outdoor = raf.ReadInt(raf.CurrentPosition)
raf.Close
End If
Here is the file write code that doesn't seen to be working:
B4X:
Dim raf As RandomAccessFile
raf.Initialize(File.DirRootExternal,"cb2.dat",False)
raf.WriteInt(msb_Nightime,0)
raf.WriteInt(msb_Indoor,raf.CurrentPosition)
raf.WriteInt(msb_Outdoor,raf.CurrentPosition)
raf.Close
I think that is the problem. The code is working but the reference to WHERE the file is located is the mix up. Originally I was trying to place the file in the File.DirInternal location but then I could not see the file to get access to it so someone told me to move it to File.DirRootExternal where I can get to it.
So now I see that the file read is reading from the .Internal file which is there but I don't use anymore and my write code is correctly writing to the .External location -- a mis-match. I'll change that one line to read from File.DirRootExternal and I think that will work.
Thanks for your keen eye...!!!
(I'd like to delete that File.DirInternal file but I've been told I cannot reach it to delete it).
Am I able to mix writing integers to a file along with strings? Below I want to add writing a string to this file but the File.WriteString function seems to want to write a string to the whole file...is that correct? Or can I mix writing Intergers and Strings?
Dim raf As RandomAccessFileraf.Initialize(File.DirRootExternal,"cb2.dat",False)raf.WriteInt(msb_Nightime,0)
raf.WriteInt(msb_Indoor,raf.CurrentPosition)
raf.WriteInt(msb_Outdoor,raf.CurrentPosition)
raf.Close