Android Question error with File.GetText

jt2010

Member
Licensed User
Longtime User
Hi. Can you help me with this code:

Sub sample
Dim read,read1 As String
read=File.GetText(File.DirAssets,"cfg.txt")
Msgbox(read,"Testing")' value is file.txt and is ok
read1=File.GetText(File.DirAssets,read)
' Error
' java.io.FileNotFoundException: /data/user/0/direction.slide.id/files/virtual_assets/file.txt: open failed: ENOENT (No such file or directory)
End Sub

I cannot use a variable to use with File.GetText and load a file, but if i use a predefined value (ex: "cfg.txt") it works….any suggestion?
thanks,
Jose
 

npsonic

Active Member
Licensed User
B4X:
Log(File.ReadString(File.DirAssets,File.ReadString(File.DirAssets,"cfg.txt")))
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I cannot use a variable to use with File.GetText and load a file, but if i use a predefined value (ex: "cfg.txt") it works….any suggestion?
This is how your code should be:
B4X:
Dim read As String
    Dim read1 As String="newdbfiledate.txt"
    read=File.GetText(File.DirAssets,read1)
    Log("first time: " & read)  'displays the content of file
   
    read=File.GetText(File.DirAssets,"newdbfiledate.txt")
    Log("second time: " & read)   'displays the same content of file as above
 
Upvote 0

jt2010

Member
Licensed User
Longtime User
hi guys. thanks for the anwsers but the question is how could i use File.GetText to load a text string with a variable value and not a predefined value;

This works:
read=File.GetText(File.DirAssets,"somefile.txt") ' uses a predefined value with comma

this doesn't:
read1=File.GetText(File.DirAssets,read) ' uses a variable
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
read1=File.GetText(File.DirAssets,read) ' uses a variable
What is the content of read ?
If it's "somefile.txt" it will work !
If it's the result of read=File.GetText(File.DirAssets,"somefile.txt"), it will for sure not work.
Because, in that case, read, is the content of the file and not it's name.
But, unfortunately you don't give enough information.
 
Upvote 0

jt2010

Member
Licensed User
Longtime User

Klaus, but the variable read1 assumes the string value that was in variable read (somefile.txt) isn't so? it´s the content of file and the filename of the next text file that i want to read;

In another way-..how could i use File.GetFile to do this:
i have two text file that contains only one line with the text:

File1
=========
file1.txt

file2
=========
file2.txt

i want to read the first text file, and put the value stored in a variable (file1.txt);
then use File.GetText to load the content of the file1.txt not with the syntax: File.GetText(File.DirAssets,"somefile.txt") but with File.GetText(File.DirAssets,variablename)

is it possible?
thanks
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
is it possible?
This will definitely work:
B4X:
  'Added 2 files to assets: file.txt where its content is: content1.txt and a file named content1.txt with its content: this is a test
    Dim read,read1 As String
    read=File.GetText(File.DirAssets,"file1.txt")   'its content is: content1.txt 
    read1=File.GetText(File.DirAssets,read)         'its content is: this is a test
    Log(read)   'displays: content1.txt
    Log(read1)  'displays: this is a test
However, if the content of file1.txt has a variable with a comma, it will not work because you cannot have a file name with a comma in the name. It will return the error you were showing in your first post.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…