How to set Encoding in Java

0 votes

How do I properly set the default character encoding used by the JVM (1.5.x) programmatically?

I have read that -Dfile.encoding=whatever used to be the way to go for older JVMs... I don't have that luxury for reasons I wont get into.

I have tried:

System.setProperty("file.encoding", "UTF-8");

And the property gets set, but it doesn't seem to cause the final getBytes call below to use UTF8:

    System.setProperty("file.encoding", "UTF-8");

    byte inbytes[] = new byte[1024];

    FileInputStream fis = new FileInputStream("response.txt");
    fis.read(inbytes);
    FileOutputStream fos = new FileOutputStream("response-2.txt");
    String in = new String(inbytes, "UTF8");
    fos.write(in.getBytes());
May 29, 2018 in Java by developer_1
• 3,350 points
16,262 views

1 answer to this question.

0 votes

Unfortunately, the file.encoding property has to be specified as the JVM starts up; by the time your main method is entered, the character encoding used by String.getBytes() and the default constructors of InputStreamReader and OutputStreamWriter has been permanently cached.

As Edward Grech points out, in a special case like this, the environment variable JAVA_TOOL_OPTIONS can be used to specify this property, but it's normally done like this:

java -Dfile.encoding=UTF-8 … com.x.Main

Charset.defaultCharset() will reflect changes to the file.encoding property, but most of the code in the core Java libraries that need to determine the default character encoding do not use this mechanism.

When you are encoding or decoding, you can query the file.encoding property or Charset.defaultCharset() to find the current default encoding, and use the appropriate method or constructor overload to specify it.

answered May 29, 2018 by Rishabh
• 3,620 points

Related Questions In Java

0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,140 points
1,229 views
0 votes
1 answer

How to set AUTO-SCROLLING of JTextArea in Java GUI?

Hii kartik, When you click anywhere over JTextArea, ...READ MORE

answered Apr 21, 2020 in Java by Niroj
• 82,840 points
2,483 views
0 votes
1 answer

How to set path if java is installed in E drive or D drive rather than c

Hello, @Amazing, It doesn't really matter where you ...READ MORE

answered Sep 1, 2020 in Java by Roshni
• 10,480 points

edited Jul 4, 2023 by Khan Sarfaraz 4,445 views
0 votes
1 answer

How to convert a Set to List in Java?

Hi@MD, The most straightforward way to convert a ...READ MORE

answered Dec 30, 2020 in Java by akhtar
• 38,260 points

edited Jun 22, 2023 by Khan Sarfaraz 621 views
0 votes
1 answer

How to encode data using Base64 in Java?

import org.apache.commons.codec.binary.Base64; We can not use sun.* packages ...READ MORE

answered May 30, 2018 in Java by Sushmita
• 6,920 points
1,075 views
0 votes
1 answer

Java URL encoding of query string parameters

I would not use URLEncoder. Besides being incorrectly ...READ MORE

answered Jun 1, 2018 in Java by Rishabh
• 3,620 points
17,602 views
0 votes
1 answer

How to perform Java 256 bit AES Password Based Encryption?

/* Derive the key, given password and ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 3,190 points
1,670 views
0 votes
1 answer

Integrate UTF-8 encoding in Java WebApp

Mostly characters äåö are not a problematic ...READ MORE

answered Dec 12, 2018 in Java by Sushmita
• 6,920 points
12,795 views
0 votes
3 answers

How to parse JSON in Java

import org.json.*; JSONObject obj = new JSONObject(" .... ...READ MORE

answered Aug 20, 2018 in Java by Daisy
• 8,140 points
4,044 views
0 votes
1 answer

How to pad string in Java?

Padding to 10 characters: String.format("%10s", "foo").replace(' ', '*'); String.format("%-10s", ...READ MORE

answered May 1, 2018 in Java by Rishabh
• 3,620 points
1,049 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP