Problem on Compiling

Hello together,

I have the following code

<code>

 
private void writeExportLogFile() throws IOException {
   File file = new File(pathToFile, fileName+".LOG");
   StringBuilder fileWriter = new StringBuilder();
   fileWriter.append("Tabelle: " + tableName + "\r\n");
   fileWriter.append("Anzahl exportierter Datensätze: " + exportedRows + "\r\n");
 
   fileWriter.append("Initialwerte für sonstige Felder unterdrücken: " + "\r\n");
 
   
 
      FileUtils.writeStringToFile(file, fileWriter.toString(), codepage);
 
}

</code>

when i compile this class and run it the ä and ü charcters didn't get printed to the file. There is only a ?
When i compile and run it with eclipse everything is fine. I compile it with JDK 1.6
The FileUtils class is from Apache commons IO.

Do you have any suggestions on what could be wrong or where i should look? Ask for any more informations if you need any.

Thank you in advance.

3 comments
Comment actions Permalink

Hello.

Is your source encoded in UTF-8? Then please make sure that Settings dialog / Editor / File Encodings / Project Encoding = UTF-8. (Same applies to any encoding actually.)

BTW, what IDEA build do you use? Recently we have fixed this: https://youtrack.jetbrains.com/issue/IDEA-131602

Regards,
Alexander.

0
Comment actions Permalink

You have a character encoding issue. A different encoding is being set when IDEA runs it versus when Eclipse runs it. While you can solve this in the IDE, it would be better to solve it in the code. That way regardless of where it is run, you are ensured to have the correct encoding.

I see that you are passing a 'codepage' value into the FileUtils.writeStringToFile() method. How are you setting that value? If you set it directly to "UTF-8" (or your desired character encoding), it should work as desired. If however you are setting it by way of the system encoding, then you may get different results. For example, on my PC when I run your code (on the command line outside of an IDE) using

FileUtils.writeStringToFile(file, fileWriter.toString(), Charset.defaultCharset().name());


I get placehoders for the accented characters.However, if I run the code:

FileUtils.writeStringToFile(file, fileWriter.toString(), "UTF-8");


I get the proper characters. Alternatively, I can run the first code example (using the default Charset) but set the file.encoding system property to UTF-8:

>java -Dfile.encoding="UTF-8"  -classpath "path/to/lib/commons-io-2.1.jar;" com.example.FileExample


And then it uses the UTF-8 encoding.


What is likely happening for you is that Eclipse is setting  the file.encoding system property when it runs your code. You can set IDEA to do the same. One option is to set it in the run/debuf configuration you are using to run the code. On the "Configuration" tab of the run/debug configuration, in the "VM Options" field, add the value -Dfile.encoding="UTF-8". If you want this value to be set by default for any new run/debug configurations, you can set it on the "Application" configuration (and any others that may need it like JUnit, Tomcat, etc.) under the "Defaults" node on the left in the Run/Debug Configuration dialog.

The other option would be to go into File > Settings > Editor > File Encodings. At the top, set the "Project Encoding" to "UTF-8". IDEA will now add the -Dfile.encoding="UTF-8" to the command line when it runs any code.

0
Comment actions Permalink

thank you for your quick help. It was an encoding problem. Intellij showed me the correct encoding but somehow it was in another encoding after reinstalling i saw that the file was in another encoding than it should be.

0

Please sign in to leave a comment.