[sun java] conver charset for string

R

Rafal \(sxat\)

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
 
J

Joshua Cranmer

Rafal said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top