We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Special Characters lost when using loadstring
Page Index Toggle Pages: 1
Special Characters lost when using loadstring (Read 872 times)
Special Characters lost when using loadstring
Aug 25th, 2009, 1:33pm
 
Im still have some problems with my loading the txt file. I thought i open up a new topic cause it could be useful for others too.

I save a string to a textfile and when i load it again all special characters are lost. Or better not loaded, cause they are saved to the txt file proberly. so it must be a loading problem i guess.
Does anybody know why and how to avoid it ?

hier is a code snipped to test it:

Code:


String string = "random Text ü ä ö ß etc.";
BufferedWriter bw = null;
try
{
FileWriter fw = new FileWriter("C:/list.txt"); // true means: "append"
bw = new BufferedWriter(fw);
bw.write(string);
}
catch (IOException e)
{
// Report problem or handle it
}
finally
{
if (bw != null)
{
try {
bw.close();
}
catch (IOException e) {
}
}
}


String lines[] = loadStrings("C:/list.txt");
println(string);
println(lines[0]);
Re: Special Characters lost when using loadstring
Reply #1 - Aug 25th, 2009, 2:28pm
 
Could it be an encoding issue

Scite (text/code editor) says the saved file is in 8 bit; and if I switch it to anything else (e.g. UTF8) I get gobbledigook instead of special characters...  In fact looking at the loadStrings() documentation I notice:

Starting with Processing release 0134, all files loaded and saved by the Processing API use UTF-8 encoding. In previous releases, the default encoding for your platform was used, which causes problems when files are moved to other platforms.

Since it looks like you're writing the text file with pure Java I guess it may use the platform's default encoding, which would presumably cause your problem...
Re: Special Characters lost when using loadstring
Reply #2 - Aug 25th, 2009, 3:03pm
 
i read that too, but I was wondering why there should be a problem then. Doesnt UFT8 include all these chars ? http://www.utf8-chartable.de/

Like i said, writing seems not to be the problem cause the data is stored correctly in the txt file when i open it with notepad. Only when read they seems to get lost.
Re: Special Characters lost when using loadstring
Reply #3 - Aug 25th, 2009, 3:16pm
 
you can set your own encoding like this:

Code:

...
try
{
 OutputStream fi = createOutput("/tmp/list.txt");
 OutputStreamWriter output = new OutputStreamWriter(fi, "UTF-8");
 bw = new BufferedWriter(output);
 bw.write(string);
}
catch (IOException e)
{
... patatipatata ...


in fact, if you set the writer's encoding to anything else, like ascii, you'll get gibberish back.
Re: Special Characters lost when using loadstring
Reply #4 - Aug 25th, 2009, 3:23pm
 
Well I must admit my understanding of the subtleties of encoding is minimal (this article seems fairly thorough though!) but it would seem to me that if something is stored with one type of encoding then it's not necessarily going to be directly readable as another type.  I guess a good analogy might be different image file types:  If you change the file extension in Windows and tell it that a jpeg is a gif you might not expect it to open successfully Wink

When I open the text file in Scite it tells me the encoding is "8-bit", rather than specifically UTF-8...  which suggest to me that "8-bit" is something other than UTF-8; so I think it may be worth looking into this a bit further.  In fact this link looks like it might provide some clues Wink

Ah - I see someone more knowledgeable in this area has already come back with an answer Smiley
Re: Special Characters lost when using loadstring
Reply #5 - Aug 25th, 2009, 3:39pm
 
Thx to both of you!

Alvaro, that looks great and seems to work in this example i made. But in the actual code im using i need to append the string to a textfile.
Explained here what i mean : http://processing.org/discourse/yabb2/num_1251025598.html#0

but now as you use outputstreamwriter instead of filerwriter i can not append it anymore. and to bad cant change the encoding like you did when using the filewriter. is there a way to combine it somehow
Re: Special Characters lost when using loadstring
Reply #6 - Aug 25th, 2009, 7:49pm
 
hmm i wrote

OutputStream fi = new FileOutputStream("data.txt", true);
OutputStreamWriter output = new OutputStreamWriter(fi, "UTF-8");


and it seems to work. Anybody with knowledge, was that ok?!
Page Index Toggle Pages: 1