Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java code to convert a file from UTF-8 to ISO8859-1

5,673 views
Skip to first unread message

carlito

unread,
Dec 14, 2008, 5:42:45 PM12/14/08
to
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

unread,
Dec 14, 2008, 5:57:36 PM12/14/08
to
On Dec 15, 9:42 am, carlito <kfou...@gmail.com> wrote:
> Any java code out there to do a similar job as the one iconv does ?

Use an InputStreamReader/Writer with an
appropriate Charset.

> Thanks a lot for your help.

No worries.

--
Andrew Thompson
http://pscode.org/

Mark Space

unread,
Dec 14, 2008, 7:35:41 PM12/14/08
to

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

unread,
Dec 14, 2008, 11:26:56 PM12/14/08
to
On Sun, 14 Dec 2008 14:42:45 -0800 (PST), carlito <kfo...@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>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

unread,
Dec 15, 2008, 6:09:28 PM12/15/08
to
Thanks a lot Andrew, Mark and Roedy for your help. I am going to
implement your ideas right away.

Thanks again.

Khalil FOUNDY

Roedy Green

unread,
Oct 13, 2009, 5:56:19 PM10/13/09
to
see http://mindprod.com/jgloss/encoding.html

You can also ask the FileIO Amanuensis to generate you some code to
read/write in any encoding.

see http://mindprod.com/applet/fileio.html


--
Roedy Green Canadian Mind Products
http://mindprod.com

I advocate that super programmers who can juggle vastly more complex balls than average guys can, should be banned, by management, from dragging the average crowd into system complexity zones where the whole team will start to drown.
~ Jan V.

veronabalca...@gmail.com

unread,
Oct 29, 2017, 11:03:53 AM10/29/17
to
0 new messages