B4J Question UTF-8 and UTF-8 without BOM

Discussion in 'B4J Questions' started by imbault, Jul 22, 2015.

  1. imbault

    imbault Well-Known Member Licensed User

    Hi all the community,

    I've a weird problem:

    I want to upload a CSV file via FTP, so far, no problem:

    The pb is the target, a SAP integration system, which wants pure UTF-8 encoded file, and it seems (accordingly to the SAP IT people), I send a UTF-8 without BOM file.

    So I tried to translate to file with :
    Code:
    tr.Initialize(File.OpenInput(cDir, cFileIn))
    tw.Initialize2(
    File.OpenOutput(cDir, cFileOut,False),"UTF8")
    and it's still in UTF-8 without BOM, Notepad++ shows it: (it should be Encoder en UTF-8, the line below)

    Any idea, guys???

    upload_2015-7-22_8-16-52.png
     
  2. LucaMs

    LucaMs Expert Licensed User

    ehm... I'm sure you used also the commands to read and write the file, not only the initializations, right?
     
  3. imbault

    imbault Well-Known Member Licensed User

    To create the cFileIn: StringUtils SaveSCV2, why?
    Code:
    su.SaveCSV2(File.DirApp,cFileIn,";",li,myHeaders)
     
  4. LucaMs

    LucaMs Expert Licensed User

    Try:
    Code:
    tw.WriteList(tr.ReadList)
    tw.Flush
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Do you want to add the BOM? Are you sure that it expects the BOM header?
     
  6. imbault

    imbault Well-Known Member Licensed User

    He expects a pure UTF-8 CSV file, and not a UTF-8 Without BOM file, like Notepad++ shows...
    I can say I'm pretty confused, cause I thaught StringUtils SaveSCV2, in B4J, was supposed to create a UTF-8 file...

    So I'm lost with those UTF-8, UTF-8 Without BOM...
     
  7. imbault

    imbault Well-Known Member Licensed User

    Thanks Luca, so I tried:
    Code:
    tr.Initialize(File.OpenInput(cDir, cFileIn))
    tw.Initialize2(
    File.OpenOutput(cDir, cFileOut,False),"UTF8")


    tw.WriteList(tr.ReadList)
    tw.Flush
    And when I open it in Notepad++, always encoding with UTF-8 (without BOM), not UTF-8
    I'm getting nuts

    Patrick
     
  8. LucaMs

    LucaMs Expert Licensed User

  9. Erel

    Erel Administrator Staff Member Licensed User

    Standard UTF 8 text files do not include the BOM header. B4J (as well as B4A and B4i) will not add this header.

    You can use this code to create a text file that starts with this header:
    Code:
    Dim out As OutputStream = File.OpenOutput(...)
    out.WriteBytes(
    Array As Byte(0xEF,0xBB,0xBF), 03)
    Dim tw As TextWriter
    tw.Initialize(out)
    'write the text with tw
    tw.Close
     
    rwblinn, billzhan and imbault like this.
  10. Bob Sabrook

    Bob Sabrook Member Licensed User

    .. or if you like File.WriteString

    Code:
    Public Sub Export_(dir_ As String, File_ As String, content As String)
      
    Dim rows  As StringBuilder
      rows.Initialize
      
    Dim bytes() As Byte = Array As Byte(239,187,191)
      rows.Append(
    BytesToString(bytes,0,3,"UTF8"))
      rows.Append(content & 
    CRLF )
      
    File.writestring(dir_, File_, rows.tostring)
    End Sub
     
Loading...