Writing to external storage not giving expected results

jschuchert

Active Member
Licensed User
Longtime User
The following code is what I am using (been revised numerous times) in trying to write a file so that there is separation between the lines.

B4X:
Sub btnOK_click
If chklistdisplay.Checked =True Then
Msgbox("A file named 'List of points.txt' will be created","msg")
Dim writer As TextWriter
writer.Initialize (File.OpenOutput (File.DirrootExternal ,"listofpoints8.txt",True))
End If
If txtrange.Text ="" Then 
Dim reader As TextReader 
reader.Initialize (File.openinput(File.Dirinternal ,main.strfilename ))
Dim lineoftext As String
Dim cline()
lineoftext=reader.ReadLine
Do While lineoftext<>Null
cline=Regex.Split (",",lineoftext)
txtdisplay.Text =txtdisplay.Text & cline(0) & "  " & cline(1) & "  " & cline(2) & "  " & cline(3) & CRLF
If chklistdisplay.Checked =True Then
writer.Write (cline(0) & "  " & cline(1) & "  " & cline(2) & "  " & cline(3)) '& CRLF 
writer.Write (CRLF)
End If
lineoftext=reader.ReadLine 
Loop
reader.Close
If chklistdisplay.Checked = True Then writer.Close
End If

The display looks like this and I was hoping the written text file would also.
1 1000 1000 None
2 1027.69920 1030.22837 None
3 1038.56960 1070.79725 None
4 999.93257 1081.15001 None
5 1034.91736 1100.54239 None
6 1082.14682 1126.72211 None
7 1028.14682 1126.72211 None
8 1028.14682 1141.72211 None
9 958.14682 1141.72211 None
10 941.18608 1080.52911 None

However, in notepad it looks like this:
1 1000 1000 None2 1027.699 1030.228 None3 1038.570 1070.797 None4 etc.
1126.722 None8 1028.147 1141.722 None9 . . . . .
(I have rounded the decimals for this illustration) This is what is shown when I look at the file in Notepad.

The weird thing is that when I copied and pasted from notepad, the result was as it should be (shown above with the 10 lines. But a user will only see the points joined together.

In b4ppc I was able to actually write the text display without parsing.

I am using the b4a-bridge with a tablet.

Anyone see anything that can be done differently to get the results I am looking for. I'm afraid a user would be very disappointed at seeing the file in this fashion (everything in a couple of lines). The main reason for the user being able to get a text file is so they could print the results.

Thanks, guys for all of your excellent help.

Jim Schuchert
 
Last edited:

Tom Christman

Active Member
Licensed User
Longtime User
I'm not sure this is the problem, but you have a lone ' in the writer.write line (line 17) near the end of that statement just before & CRLF. Hope this helps.
Tom
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Guess what? When I printed the notepad file, it came out with the proper display. Go figure. Maybe the problem is solved?? I will explain this anomaly in my user manual before the user panics as I did.

Jim
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
However, in notepad it looks like this:
That's because Notepad uses the windows convention of CR + LF for end of line but text files in Android use the Unix convention of LF only. Wordpad will probably display it properly. Text editors like Programmers Notepad 2 or NotePad ++ can edit both types and convert the line endings for you if required.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Andrew, that was a great observation. I could have saved several hours if I had realized that. I actually have notepad ++ but never used it (until now) and it works perfectly with my issue. I just can't say enough good about this forum and the experts like you who give your time to help those like me. Maybe someday I can give back.

Tom, good eye but in the next line I replaced that commented out 'crlf' with another one.

Jim
 
Upvote 0
Top