B4J Question Unreadble Characters

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I am reading several text files into lists, processing the info and writing them again.
One of the files, however has some special characters, tar i connot read correctly.
Any ideias ?

File:
B4X:
:· 72033258 7203325704/04/2018
:· 72033259 7203325704/04/2018
:µ 72035960 7203596104/04/2018

when I read the file, I get different chars in the lines and when I write the file I also get those char, instead of the origial ones.
I have tried using lists and a TextReader, same results.
 

KMatle

Expert
Licensed User
Longtime User
Hard without seeing your code. I always use RandomAccessFile. You can write and read any object you like (lists, maps, texts, etc.). Here it is encrypted, too.

B4X:
Dim raf As RandomAccessFile
raf.Initialize(MyPath, "config.dat", False)
raf.WriteEncryptedObject(MyList, "MySecretPW", raf.CurrentPosition)
raf.WriteEncryptedObject(MyMap, "MySecretPW", raf.CurrentPosition)
raf.Close
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hard without seeing your code. I always use RandomAccessFile. You can write and read any object you like (lists, maps, texts, etc.). Here it is encrypted, too.

B4X:
Dim raf As RandomAccessFile
raf.Initialize(MyPath, "config.dat", False)
raf.WriteEncryptedObject(MyList, "MySecretPW", raf.CurrentPosition)
raf.WriteEncryptedObject(MyMap, "MySecretPW", raf.CurrentPosition)
raf.Close

My code is a simple file.readlist.
however I am going to try your approach. thanks
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
so, to read a text file to a list but preserving its contents :

B4X:
dim raf As RandomAccessFile
raf.Initialize(somePath,someFile, False)
dim TextList as list = raf.ReadObject(raf.CurrentPosition)
raf.Close

the code gets stuck in the raf.readobject line
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Have you an example of the file you are trying to read, it could be just a simple encoding error that is preventing you from reading it.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The only way I could get it to write the same chars was to read the original as windows-1251, which makes me think this file is from a terminal type device.
B4X:
 Dim tr As TextReader
 Dim li As List
 li.Initialize
 tr.Initialize2(File.OpenInput("c:/temp","cashier.txt"),"windows-1251")
 li = tr.ReadList
 tr.Close
 For Each l As String In li
  Log(l)
 Next
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
However, no I notice something weird.
I can read the file correctly.
Then I filter some records and write the unfiltered ones to another list.
when i log this last list lines, i get all the data in the log window, but when i write the list using a textwriter, i get an empty file.

B4X:
Dim ListaCaixa as List
Dim OutPutList as List

dim tReader as TextReader
tReader.Initialize2(File.OpenInput(WinRESTPath,"cashier.000"),"windows-1251")
ListaCaixa = tReader.ReadList
tReader.Close

OutPutList.Clear
For x=0 To ListaCaixa.Size -1
    If ListaCaixa.Get(x) <> UnwantedData Then
       OutPutList.Add(ListaCaixa.Get(x))
       log(ListaCaixa.Get(x)) ' Shows the correct data
    End If
Next

' if I log the contents of OutPutList is correct

dim tWriter as TextWriter
tWriter.Initialize2(File.OpenOutput(WinRESTPath,"cashier.000",True),"windows-1251")
tWriter.WriteList(OutPutList)
tReader.Close

cashier.000 is empty, size 0
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I'm pretty sure TextWriter is buffered, so until you call tWriter.Close, nothing may be written to disk (if all fits in the buffer). Alternatively, you can call tWriter.Flush to flush the buffers to disk (without closing the file).
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
I'm pretty sure TextWriter is buffered, so until you call tWriter.Close, nothing may be written to disk (if all fits in the buffer). Alternatively, you can call tWriter.Flush to flush the buffers to disk (without closing the file).

my bad !! I noticed that i was closing tReader instead of tWriter
 
Last edited:
Upvote 0
Top