Can source code files with different encoding coexist in (the same) Java (project in Eclipse)?

我只是一个虾纸丫 提交于 2019-12-11 22:19:58

问题


I know Java uses UTF-16 internally and expects .properties files to be in ISO-8859-1 by default.

I'm currently working on a project that was written in Eclipse, whose default encoding on our systems is cp-1252. I'm thinking utf-8 would be a much more sensible option, going forward.

However, given the scale of the project (it's split up into modules and uses libraries from all over the place), I can't just batch-convert all source code files in one go.

Will Java have a problem with some files in a project being in one encoding and some in another? (Clearly, having entire libraries written in encodings that are different from one another doesn't seem to be a problem - probably because they are all UTF-16 once compiled, anyway.)

Would Eclipse be able to handle that (i.e. different encodings per file) correctly?


回答1:


Yes you can.

You can choose the default encoding to use for the whole Eclipse project:

  • right click on a project
  • resource part
  • Text file encoding zone, check Other and choose UTF-8 (or what you want) in the combo

You can also change the encoding for a particular file:

  • right click on the file
  • resource part
  • Text file encoding zone, check Other and choose UTF-8 (or what you want) in the combo

Preferences are store in the hidden folder .settings in your project. File encoding preferences are store in the .settings/org.eclipse.core.resources.prefs.

Thoses preferences can be commited using your favorite source control and shared with other developpers.



来源:https://stackoverflow.com/questions/26324708/can-source-code-files-with-different-encoding-coexist-in-the-same-java-projec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!