Getting File Not Found Exception

Guardian17

Active Member
Licensed User
Longtime User
I'm trying to access a text file. I saw something similar to this code snippet on the basic4android site:

B4X:
Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    If File.Exists(File.DirInternal, "MyTextFile.txt") = False Then
      File.Copy(File.DirAssets, "MyTextFile.txt", File.DirInternal, "MyTextFile1.txt")
    End If
  End If
.
.
End Sub

I've also added the MyTextFile.txt to the files list (accessed in the lower right corner of the screen).

The exception is:
"java.io.FileNotFoundException: mytextfile.txt"

What's the proper way to open a file after checking whether it exists, and what are the relationships between File.DirAssets, File.DirInternal, and any other file on my computer? I've read the definitions of these File-Usage terms, but obviously I do not fully understand them. Where do my own files that I want to use in the App get stored and/or loaded?

Incidentally, I did manage to get this to work in one sample project, but I then tried transferring the code to another project to try to get it to work in your Table example found here:

http://www.b4x.com/forum/additional...class-tableview-supports-tables-any-size.html

I think this Table class will work very well for a word game application I'm just starting.

If I understood the original code, when the file doesn't exist in the DirInternal location, the File.Copy copies the file from the "File.DirAssets" location (wherever that may be) into the "local" "File.DirInternal" directory (wherever that may be). But I don't know why it's not finding my text file.

Does it have anything to do with the paths defined in "Configure Paths"? (although the paths haven't changed from when I worked on the first project to this new project).
 

NJDude

Expert
Licensed User
Longtime User
A couple of things.

1- You are checking if "MyTextFile.txt" exists ONLY the very first time the app runs (you have it inside If FirstTime Then...)

2- If "MyTextFile.txt" doesn't exist you copy the file from the assets directory to the device but you name the file "MyTextFile1.txt", I'm guessing you are using this name afterwards <?>

I would try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    If File.Exists(File.DirInternal, "MyTextFile.txt") = False Then
    
       File.Copy(File.DirAssets, "MyTextFile.txt", File.DirInternal, "MyTextFile.txt")

    End If
..

End Sub
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
When you added the text file to your project's Files, did it get renamed to all lower case: mytextfile.txt?
That is generally the case even though the filename in the IDE Files tab will probably still be the original upper and lower case name.

Android file system is case sensitive so maybe that's the problem?

Martin.
 
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
Thanks for the replies. I will look at this later this evening.

Not sure why it would have worked in my first project, but not in this project, but I will check all conditions mentioned. :)
 
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
File Not Found Problem Fixed, with a follow-up for File.Exists

Getting rid of the "1" in "MyTextFile1.txt" solved the problem.:sign0060:
Thank you!

I had changed the name of the destination file when I was trying to pin down what the problem was, figuring that it would just take "MyTextFile.txt" (the source file) and write it to the destination directory as "MyTextFile1.txt" (it's just a COPY function, right? so the destination file could be called anything?) I was using the MyTextFile1.txt later in the program, which I also had to change.

The exception I had received pointed to "MyTextFile.txt", not "MyTextFile1.txt", so I figured it was a problem with the SOURCE file.

What gets me is that the first project also used the "MyTextFile1.txt" as the destination, and it worked just fine, (and still DOES)!!!:sign0080:

RELATED QUESTION -- If a file has been placed into the project's File List on the right-side pane, do I still need to bother with the "If File.Exists...False" method of checking for the presence a file, then copying it, assuming I do nothing to delete the file?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
RELATED QUESTION -- If a file has been placed into the project's File List on the right-side pane, do I still need to bother with the "If File.Exists...False" method of checking for the presence a file, then copying it, assuming I do nothing to delete the file?

It depends entirely on what you want to do with the file.
A file added to your project's Files is compiled into the APK, during compilation the file gets compressed.
Any file in File.DirAssets (files that have been compiled into the APK) are read-only and cannot be modified.
Also a file cannot be removed/deleted from File.DirAssets.

That's no problem if the file is a text file that you wish to read only, but if you want to modify that text file then you'll have to first copy it out of the File.DirAssets folder(the APK) to a location where it can be written to.

Another example: SQL databases can be included in your Files folder and compiled into the APK, but require that they be copied to a writeable folder before use.

Martin.
 
Upvote 0
Top