WriteInt Help

rfresh

Well-Known Member
Licensed User
Longtime User
I'm trying to piece together various references on file i/o. I need to write an Integer to a file and have pieced together the following:

B4X:
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "1.dat", False)   
   WriteInt (Value As Int, Position As Long)
   close

I actually will need to write 3 integer values to this file so do I make 3 calls to WriteInt()?

But theRandomAccessFile is red in my code but I don't see a File IO lib to include.

Also I didn't see any Close examples and I'm not sure it's raf.Close or Close.raf?

In this case I want to write an integer value to the internal root section with the filename 1.dat

Thanks...
 
Last edited:

admac231

Active Member
Licensed User
Longtime User
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
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thanks for the help.

Hmmm...I am getting a complier error on that line Dim raf As RandomAccessFile.
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
The library name is RandomAccessFile.

7fDVW.png
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Ahhh that one is not in my Lib listing. How do I get it in there? I have to download it from somewhere right?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Where does File.DirInternal get written to?

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?

B4X:
raf.Initialize(File.DirInternal,"cb.dat",False
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
FileNotFoundException help

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.

B4X:
raf.Initialize(File.DirRootExternal,"cb2.dat",False)

The full code is here:
B4X:
    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
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
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?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Here is the file read code:

B4X:
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
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
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).
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
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?

B4X:
File.WriteString(File.DirRootExternal, "1.txt", "Some text")


B4X:
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
 
Upvote 0
Top