[sun java] conver charset for string

Discussion in 'Java' started by Rafal \(sxat\), Apr 14, 2007.

  1. Hello

    how change charset for this string from cp1250 (Windows) to UTF-8


    all Program in java is default running in UTF-8

    File f = new File("C:/myfile.txt);
    FileReader rd = new FileReader(f);
    System.out.println("GET - encoding: " + rd.getEncoding()); /* this is
    always utf-8 why?*/
    char[] buf = new char[(int)f.length()];
    rd.read(buf);
    return new String(buf).replaceAll("[\n\r]*", "");

    myfile is charset cp1250
    how change String from cp1250 to utf 8 ?

    regards
    Rafal
    Rafal \(sxat\), Apr 14, 2007
    #1
    1. Advertising

  2. Rafal (sxat) wrote:
    > Hello
    >
    > how change charset for this string from cp1250 (Windows) to UTF-8
    >
    >
    > all Program in java is default running in UTF-8
    >
    > File f = new File("C:/myfile.txt);
    > FileReader rd = new FileReader(f);
    > System.out.println("GET - encoding: " + rd.getEncoding()); /* this is
    > always utf-8 why?*/
    > char[] buf = new char[(int)f.length()];
    > rd.read(buf);
    > return new String(buf).replaceAll("[\n\r]*", "");
    >
    > myfile is charset cp1250
    > how change String from cp1250 to utf 8 ?
    >
    > regards
    > Rafal


    From the API for java.io.FileReader:
    The constructors of this class assume that the default character
    encoding and the default byte-buffer size are appropriate. To specify
    these values yourself, construct an InputStreamReader on a FileInputStream.

    So your second line would be:
    InputStreamReader rd = new InputStreamReader(new FileInputStream(f),
    "cp1250");

    ("cp1250" or "Cp1250"? I don't actually know.)
    Joshua Cranmer, Apr 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kylin
    Replies:
    2
    Views:
    405
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Jun 2, 2005
  2. Replies:
    0
    Views:
    2,855
  3. John Muñoz

    how i conver a string to number with asp?

    John Muñoz, Nov 20, 2003, in forum: ASP General
    Replies:
    1
    Views:
    132
    Aaron Bertrand - MVP
    Nov 20, 2003
  4. optimistx

    javascript charset <> page charset

    optimistx, Aug 14, 2008, in forum: Javascript
    Replies:
    2
    Views:
    259
    optimistx
    Aug 15, 2008
  5. sout saret
    Replies:
    1
    Views:
    265
    Jim Gibson
    May 8, 2013
Loading...

Share This Page