B4J Question How to get accented character from csv File

lucdrb

Active Member
Licensed User
Longtime User
Hi,

I load a CSV file with the following code:
B4X:
    Private lstCSV As List
    lstCSV.Initialize
    Private fc As FileChooser
    fc.Initialize
    fc.SetExtensionFilter(Language.GetString("{0011}", "CSV"),Array As String("*.csv"))
    fName = fc.ShowOpen(formName)
    If fName <> "" Then
        lstCSV = strUtil.LoadCSV(fName,"",";")
    End If

To read the list I use :

B4X:
For Each row() As String In lstCSV   
private s1 as string = row(0)
private s2 as string = row(1)

Next

the row(0) contain french accented character like : Date d'opération but I got Date d'op�ration

Is there a way to get the accented character wich is in utf8 ?
Any help would be welcome.
Thank in advance

Luc
 

keirS

Well-Known Member
Licensed User
Longtime User
Hmm I thought B4X read files in as UTF8 by default. File.ReadString and File.ReadList certainly do.
So try using File.ReadList and split each item in the list.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Are you sure the file you've opened is UTF8 encoded? (try to open it with notepad++ to check that)
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
In that case this sub should work although you will still have split each line up.

B4X:
Sub ReadFileEncoded(FileName As String,Encoding As String) As List
    Dim JO  As JavaObject
    Dim FileLine As String
    Dim RowList As List
    RowList.Initialize()
    JO.InitializeNewInstance("java.io.BufferedReader",Array As Object(JO.InitializeNewInstance("java.io.InputStreamReader",Array As Object(JO.InitializeNewInstance("java.io.FileInputStream",Array As Object(FileName)),Encoding ))))
    FileLine = ""
    Dim ReadingFile As Boolean = True
    Do While ReadingFile = True
        FileLine = JO.RunMethod("readLine",Null)
        If FileLine.Length > 0 Then
          RowList.Add(FileLine)
          
        Else
            ReadingFile = False
        End If
    Loop
  
    Return RowList
  
End Sub
Usage is:

B4X:
Dim myCSV As List
    myCSV =ReadFileEncoded("C:/B4J/mycsv.txt","ISO-8859-1")
    Log(myCSV.Size)
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Many thanks.
I get the right info now.

I' ve just change the line:

B4X:
If FileLine.Length > 0 Then
by 
If FileLine <> "null" Then

The length was never 0 because of the null character returning in fileline.

Luc
 
Upvote 0
Top