Saving Value in Text File

Nyptop

Active Member
Licensed User
Longtime User
Hi Forum,

I have what I think should bee a very simple question. With the following code I am trying to save the number of pictures which have been taken by creating a textfile and in that only saving one number which says how many pictures have been taken.

B4X:
If File.Exists(File.DirInternal, "Number.txt") Then
   Dim listReader As List
   listReader = File.ReadList(File.DirInternal, "Number.txt")
   pictureNumber = listReader.Get(0)
   Else
   Dim TextWriter1 As TextWriter
    TextWriter1.Initialize(File.OpenOutput(File.DirInternal,  "Number.txt", False))
   Dim listuno As List
   listuno.Initialize
   listuno.Add(1)
   TextWriter1.WriteList(listuno)
   Dim listun As List
   listun = File.ReadList (File.DirInternal, "Number.txt")
   picno = listun.Get(0)
    TextWriter1.Close
   End If

The above code comes in activity_create. The problem I have occurs on the line 'pictureNumber = listReader.get(1). The error says 'java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 Continue?

Any ideas?

Thank you,

Neil
 

mc73

Well-Known Member
Licensed User
Longtime User
I think that you should move this line
B4X:
TextWriter1.Close
write after this one
B4X:
TextWriter1.WriteList(listuno)
You need to close the textwriter, in order data to be written to your file, at least so I suppose. So, in your case, you tried to read data, before the close command.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why don't you use File.WriteString / ReadString ?

B4X:
If File.Exists(File.DirInternal, "Number.txt") Then
 pictureNumber = File.ReadString(File.DirInternal, "Number.txt")
Else
 File.WriteString(File.DirInternal, "Number.txt", 1)
End If

The error in your code is that you try to read before closing TextWriter so the data is not yet written to the file.
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks for the feedback guys. I tried doing what you suggested mc73 but I got the same error. I tried also what you suggested, Erel, but in my program I need too use loops a number of times and the errors tell me I need to use int's as supposed to string's.

Thanks for all the help though,

Neil
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Thanks for the feedback guys. I tried doing what you suggested mc73 but I got the same error. I tried also what you suggested, Erel, but in my program I need too use loops a number of times and the errors tell me I need to use int's as supposed to string's.

Thanks for all the help though,

Neil

Did you remove your txt file from the folder before running the corrected code? Since this is in dir.internal, remove it by code, then delete the removing code and rerun your project.
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Saving a single value number and retrieving it later

Aug 4 2012
15:00 Hours

Hi

In

Sub Activity_Create(FirstTime As Boolean)


B4X:
If File.Exists (File.DirInternal,"RndCornerValue.txt") Then
   If File.Size(File.DirInternal,"RndCornerValue.txt") > 0 Then 
      CodeModule.TCVCurrentRndCornerValue = File.ReadString(File.DirInternal ,"RndCornerValue.txt") 
   Else
      CodeModule.TCVCurrentRndCornerValue = 30
      File.WriteString(File.DirInternal,"RndCornerValue.txt",CodeModule.TCVCurrentRndCornerValue)       End If
Else
   CodeModule.TCVCurrentRndCornerValue = 30
   File.WriteString(File.DirInternal,"RndCornerValue.txt",CodeModule.TCVCurrentRndCornerValue)  
End If

.
.
.
.
End Sub


In a Code Module which I named CodeModule

Dim TCVCurrentRndCornerValue As Int


Cheers,

Ray Tesluk :D
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks for all the help. Un-installing and re-installing sorted the problem. Thank you dearly.

Neil
 
Upvote 0
Top