Reputation: 13666
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?
Upvotes: 0
Views: 478
Reputation: 11911
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:
Specify the correct encoding when instanciating your Writer
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.
Upvotes: 1
Reputation: 4029
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();
Upvotes: 1
Reputation: 12880
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.
Upvotes: 0