0

I need to specify that File objects created at runtime by my Java application are saved in a specific encoding format (i.e. UTF-8). I read here that I should specify the encoding at runtime when I start the JVM.

Since I'm developing a Maven project, can I set up the pom.xml file to specify the encoding? If yes, how?

Community
  • 1
  • 1
mat_boy
  • 12,998
  • 22
  • 72
  • 116

3 Answers3

1

If you are creating files through Java Code then

Create a FileOutputStream. You can then wrap this in an OutputStreamWriter, which allows you to pass an encoding in the constructor.

Then you can write your data to that. Try this:

FileOutputStream fos = new FileOutputStream("test.out");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Writer out = new BufferedWriter(osw);
...
out.write(ch);
...
out.close();
Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
  • Yes, of course. This is an intelligent solution, so +1. But it is not the solution to the problem. – mat_boy Nov 27 '13 at 14:57
  • you can not setup character encoding in the POM for files generated through your program logic at runtime. – Ahsan Shah Nov 27 '13 at 14:59
1

Since you want to set the runtime-encoding: no, you can't do this via maven alone since it only modifies buildtime behaviour.

Since the requirement is specified and perhaps not too likely to vary I can think of two options:

  1. Specify the correct encoding when instanciating your Writer

  2. Use System.setProperty("file.encoding","UTF-8") to specify a global encoding for your application

I would recommend 1. since it gives you finer control and won't affect other code running in the same VM. Instead of hardcoding the encoding you could also move it to some proerty in a config-file - and if you do generate it with maven you could easily switch the encoding by modifying the pom.

piet.t
  • 11,718
  • 21
  • 43
  • 52
0

To set the project encoding, You can do it this way in your pom.xml

<project>
  ...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  ...
</project>

When you want to set the encoding for the file created at runtime, as per my knowledge, there is no option to provide in Maven configuration. the link you have in question has the way of creating file with UTF-8.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • This is for the project files, not for files created at runtime! – mat_boy Nov 27 '13 at 14:39
  • Since I'm developing a Maven project, can I set up the pom.xml file to specify the encoding? If yes, how? this confused me. – Keerthivasan Nov 27 '13 at 14:42
  • See you are trying to create a file at runtime with UTF-8 encoding.As per my knowledge, you cannot give it in maven configuration – Keerthivasan Nov 27 '13 at 14:43
  • Did you read only this part or also the first part of the question? In brief, I need to specify that File objects created at runtime are saved in UTF-8. I read in another post that I this can be done only by specifing the encoding at runtime when I starting the JVM. Can I do this setting in the pom file? – mat_boy Nov 27 '13 at 14:44