Android Question How can I avoid List.Sort adding blank line to end of text file?

TonyVerberne

Member
Licensed User
I have successfully used List.Sort to sort a list of items in a text file but the process adds a blank line at the end of the file. Can I avoid this? Please forgive me if this is a very simple question?

This is the relevant code:

Sub EditText3_EnterPressed
outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
outFile.WriteLine(CRLF & Text2)
outFile.Close
List1 = File.ReadList(File.DirDefaultExternal, Text3)
'Sort file into descending alphabetical order
List1.Sort(True)
File.WriteList(File.DirDefaultExternal, Text3, List1)
End Sub
 

eurojam

Well-Known Member
Licensed User
Tony,
I think that comes from the first write action you perform in the file, where you write CRLF & Text2, which will produce an empty line and then text2.
change the code to:
B4X:
If File.Exists(File.DirDefaultExternal, Text3) Then
  outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
  outFile.WriteLine(CRLF & Text2)
else
  outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
  outFile.WriteLine(Text2)
end if
not tested, just an idea

Cheers
Stefan
 

eurojam

Well-Known Member
Licensed User
Yes you are right, the same thing. I think this is because WriteLine performs: "mytext" & Chr(13) & Chr(10) so at the end of your file will be an empty line. The only thing to avoid this , is may be loop through your list manually and for the last item using outFile.Write(Text2) instead of outFile.WriteLine(Text2)...
 

TonyVerberne

Member
Licensed User
Yes you are right, the same thing. I think this is because WriteLine performs: "mytext" & Chr(13) & Chr(10) so at the end of your file will be an empty line. The only thing to avoid this , is may be loop through your list manually and for the last item using outFile.Write(Text2) instead of outFile.WriteLine(Text2)...
I will also try that. You seem to be a Pearl Jam fan (like me)
 

TonyVerberne

Member
Licensed User
After a little more investigation it's the first three lines of the code which generate the blank line:

outFile.Initialize(File.OpenOutput(File.DirDefaultExternal, Text3, True))
outFile.WriteLine(CRLF & Text2)
outFile.Close

Oddly when I use the following code:

"List1 = File.ReadList(File.DirDefaultExternal, Text3)
'Sort file into descending alphabetical order
List1.Sort(True)
File.WriteList(File.DirDefaultExternal, Text3, List1)
ls = List.Size"

to determine the number of lines (ls) it reports a value that does not include the blank line.
 
Top