Discussion:
java code to convert a file from UTF-8 to ISO8859-1
(too old to reply)
carlito
2008-12-14 22:42:45 UTC
Permalink
Any java code out there to do a similar job as the one iconv does ?
iconv -f UTF-8 -t ISO-8859-1 -o output.csv input.csv

Thanks a lot for your help.
Andrew Thompson
2008-12-14 22:57:36 UTC
Permalink
Post by carlito
Any java code out there to do a similar job as the one iconv does ?
Use an InputStreamReader/Writer with an
appropriate Charset.
Post by carlito
Thanks a lot for your help.
No worries.

--
Andrew Thompson
http://pscode.org/
Mark Space
2008-12-15 00:35:41 UTC
Permalink
Post by carlito
Any java code out there to do a similar job as the one iconv does ?
iconv -f UTF-8 -t ISO-8859-1 -o output.csv input.csv
Thanks a lot for your help.
The Java String class will do this.

String s = new String( bytes, "UTF-8" );

where "bytes" is a byte[] of the raw UTF-8 codepoints. Convert the
variable s to ISO-8859 with

byte[] winCode = s.getBytes( "ISO-8859-1" );

"winCode" now contains Latin1 codepoints. You may have to check those
exact charset names, I didn't compile those lines.

<http://java.sun.com/javase/6/docs/api/java/nio/charset/Charset.html>


Andrew's suggestion to use InputStreamReader/OuputStreamWriter seems
good too, if you are dealing with streams.

InputStreamReader in = new InputStreamReader( is, "UTF-8" );

will read a UTF-8 formatted InputStream "is" to Java's own internal
String format. OutputStreamWriter is similar.

Java's *internal* format is always the same. Java doesn't deal with
other codepoints directly, just as arrays of bytes (i.e., arrays of
data). You'll always have to go to Java's internal format first, then
to whichever output you want, asfaik.
Roedy Green
2009-10-13 21:56:19 UTC
Permalink
This post might be inappropriate. Click to display it.
Roedy Green
2008-12-15 04:26:56 UTC
Permalink
Post by carlito
Any java code out there to do a similar job as the one iconv does ?
iconv -f UTF-8 -t ISO-8859-1 -o output.csv input.csv
see http://mindprod.com/applet/fileio.html

read the entire file into String then write it out.

If the file is too big, see http://mindprod.com/products1.html#HUNKIO

see http://mindprod.com/jgloss/encoding.html
for background.

You can also do it with the native2ascii utility.
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
carlito
2008-12-15 23:09:28 UTC
Permalink
Thanks a lot Andrew, Mark and Roedy for your help. I am going to
implement your ideas right away.

Thanks again.

Khalil FOUNDY
v***@gmail.com
2017-10-29 15:03:51 UTC
Permalink
Post by carlito
Any java code out there to do a similar job as the one iconv does ?
iconv -f UTF-8 -t ISO-8859-1 -o output.csv input.csv
Thanks a lot for your help.
Loading...