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

Discussion in 'Android Questions' started by TonyVerberne, Apr 13, 2015.

  1. TonyVerberne

    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
     
  2. eurojam

    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:
    Code:
    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
     
  3. TonyVerberne

    TonyVerberne Member Licensed User

    Unfortunately Stefan that modification did not work - I still get the blank line at the end of the file.
     
  4. eurojam

    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)...
     
  5. Troberg

    Troberg Well-Known Member Licensed User

    Or, if you are in control of the reading of the file as well, simply ignore blank lines when reading (assuming blank lines are never a valid value).
     
    TonyVerberne likes this.
  6. TonyVerberne

    TonyVerberne Member Licensed User

    I will also try that. You seem to be a Pearl Jam fan (like me)
     
  7. TonyVerberne

    TonyVerberne Member Licensed User

    Will also try that.
     
  8. TonyVerberne

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice