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 & HelpOther Libraries › Unicode problem while writing out to a text file
Page Index Toggle Pages: 1
Unicode problem while writing out to a text file? (Read 341 times)
Unicode problem while writing out to a text file?
Feb 18th, 2008, 3:45am
 
I've run into a little problem while writing a program to auto-generate French vocabulary lists for myself. I'm able to read unicode characters in and write them to the console just fine, but when I try to write a text file, something bad happens to any extended characters.

Here's a little code sample to illustrate:

String[] test = new String[2];

test[0] = "é, ö";
test[1] = "\u00EA";

println(test[0]);
println(test[1]);

saveStrings( "test.txt", test);


It prints"é, ö", and "ê" to the console very nicely, and then writes some square root symbols followed by other extended characters to the file.

Can anyone shed some light here? Am I missing some unicode output magic?



Re: Unicode problem while writing out to a text fi
Reply #1 - Feb 18th, 2008, 4:55am
 
I can't say why saveStrings() doesn't work (i checked the processing source and it seems it should), but here's some code for you that will work (uses java.nio).  

Code:

import java.nio.*;
import java.nio.channels.*;

void setup() {
String test = "é, ö \u00EA";
try {
FileOutputStream fos = new FileOutputStream(sketchPath("test2.txt"));
FileChannel outfc = fos.getChannel();
ByteBuffer bb = ByteBuffer.wrap(test.getBytes());
outfc.write(bb);
outfc.close();
}
catch (Exception e) {
e.printStackTrace();
}
}


Re: Unicode problem while writing out to a text fi
Reply #2 - Feb 19th, 2008, 3:04am
 
I don't really understand how it works, but it does. Thanks so much!
Page Index Toggle Pages: 1