B4J Question UTF-8 and UTF-8 without BOM

imbault

Well-Known Member
Licensed User
Longtime 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 :
B4X:
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
 

imbault

Well-Known Member
Licensed User
Longtime User
To create the cFileIn: StringUtils SaveSCV2, why?
B4X:
su.SaveCSV2(File.DirApp,cFileIn,";",li,myHeaders)
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime 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...
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Try:
B4X:
tw.WriteList(tr.ReadList)
tw.Flush
Thanks Luca, so I tried:
B4X:
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
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
B4X:
Dim out As OutputStream = File.OpenOutput(...)
out.WriteBytes(Array As Byte(0xEF,0xBB,0xBF), 0, 3)
Dim tw As TextWriter
tw.Initialize(out)
'write the text with tw
tw.Close
 
Upvote 0

Bob Sabrook

Member
Licensed User
Longtime User
.. or if you like File.WriteString

B4X:
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
 
Upvote 0
Top