Android Question Reading / Writing text files and where is the Files Tab?

wstein25

Member
Licensed User
I am attempting to read and write to a singe line text file. The program crashes with "file not found" error. I created a text file with Windows XP accessory Notepad. Could not find the "Files Tab", so instead used the IDE menu Windows->File Manager to include the file in the project.

I was able to find the file using Android utility file manager. Its path is:

sdcard/Android/data/b4a.BreezeView/files/virtual_assets/LastID.txt

I tried using both this path, and the recommended path: File.DirRootExternal

In all cases the commands File.ReadString and File.WriteString fail and crash the program.
 

wstein25

Member
Licensed User
I'm confused by your answer. The various tutorials and threads say "Files Tab is located in the lower right corner of the IDE"
Not on my version of the IDE. Has this changed? So, was my alternate use of the File Manager menu the correct thing to do?
My file appears in that check box window, I check it and click on Sync.

The error message is file not found.

Perhaps more to the point, what should I be doing to be able to read a 4 character text file on program launch, that contains an
ID number needed for my program to operate properly. I tried using StateManager, which did ostensibly fill in a WriteText box
but did not work when I had the test:

If Sensor_ID = Main.Entered_ID Then

The EditText box was properly re-populated but the If - Then was interpreted as false. I ditched the StateManager, it seemed way
overkill for may app anyway. And now I am struggling with finding the file so that I can read it at launch, and write it when the
user changes the text in the WriteText box.

Thanks

Bill
 
Upvote 0

wstein25

Member
Licensed User
I need to be able to write to the file the 4 characters entered in a text box.
DirAssets is read only? What directory can I use for R/W?

Sub EditSensorID_EnterPressed
Log(Entered_ID)
File.WriteString(File.DirAssets, "LastID.txt",Entered_ID)
End Sub

results in:

Error on line 86
Assets folder is read only

Thanks

Bill
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Decide where you want to store the file for write purposes , first check if it already exists and if not copy from Assets folder.
The check for file existence would only be done once on app start.
B4X:
If File.Exists(File.DirInternal, "LastID.txt") = False Then
   File.Copy(File.DirAssets, "LastID.txt", File.DirInternal, "LastID")
End If
File.WriteString(File.DirInternal, "LastID.txt",Entered_ID)

See here for other File Methods / Dir storage choices https://www.b4x.com/android/help/files.html#file
 
Last edited:
Upvote 0

wstein25

Member
Licensed User
Thanks mj. You got me started on the right path. (sorry for the pun)
I'm restoring an ID number the user entered on their last use of the app.
Here how it ended up:

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
If File.Exists(File.DirInternal, "LastID.txt") = False Then
File.Copy(File.DirAssets, "LastID.txt", File.DirInternal, "LastID.txt")
End If
Entered_ID = File.ReadString(File.DirInternal, "LastID.txt")
EditSensorID.text = Entered_ID
Log (Entered_ID)
End Sub

And it works beautifully. Thanks again

Bill
 
Upvote 0
Top