DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Tools
  4. New Approach to Encoding in IntelliJ IDEA

New Approach to Encoding in IntelliJ IDEA

Irina Megorskaya user avatar by
Irina Megorskaya
·
May. 16, 08 · Interview
Like (0)
Save
Tweet
Share
19.49K Views

Join the DZone community and get the full member experience.

Join For Free

EAP of IntelliJ IDEA version 8.0 came with numerous new features, and the new approach to encoding is one of them.

Formerly, encoding used to be a global setting that applied to all projects. Since EAP of the version 8.0, IntelliJ IDEA recognizes encoding of a file by its content, whether a file contains BOM, an explicit encoding declaration, or UTF-encoded characters, and suggests individual approach to encoding on the project level.

As a seasoned user of IntelliJ IDEA, you certainly have projects, which you would like to reuse with the new version of the product. While in the previous versions of IntelliJ IDEA the default encoding was used to open files that didn’t contain encoding information, now the files of a legacy project get another encoding which may be quite different from the old one. This is why thorough configuration of encoding becomes so important. Let’s try to deal with encoding hands on.

First, let’s define the default encoding of a project in the File/Directory Options dialog (File | Settings - Project Settings). This default encoding is a character set that will be used to create new files, unless their templates contain embedded encoding information. For example, we’ll select windows-1251 as the default project encoding:

[img_assist|nid=2925|title=|desc=|link=none|align=left|width=561|height=305]

 

 

 

 

 

 

 

 

 

 

 

 

 

Moreover, individual encoding can be defined for each directory or even a separate file that doesn’t contain encoding information within. Note that embedded encoding information overrides the project or directory settings.

Now let’s see how IntelliJ IDEA treats encoding of the different files, and how you can modify it to best suit your purposes. In our sample project we’ll create a Java class, a plain text file, HTML. XTML and JSP files: myClass.java, myTextFile.txt, myHtmlFile.html, myXhtmlFile.xhtml, and myJsp.jsp. All these files are created by templates, and the further behavior depends on whether encoding is defined in the file template or not.

Besides files of these types, there can be the files where encoding is defined by BOM. It is rather unlikely that you will produce such files in IntelliJ IDEA, but they can come from third-party tools, or be a result of some operations with encoding in the file system, and appear to be a part of your project. In our example, this is the file myTextFileWithBOM.txt.

When you open these files for editing, encoding of each one is reflected in the Status bar, and you can see that sometimes the modification of encoding is allowed, and sometimes it is prohibited. Let us study our examples in details:

 

  • myClass.java, myHtmlFile.html, and myTextFile.txt have encoding windows-1251, which is the project default, and is editable both in the Status bar, and in the File/Directory Options dialog.

    Templates of the Java, HTML and text files do not contain embedded encoding information, and thus the files created by these templates accept the project or directory defaults, which in our case is windows-1251. If for some reason you are not happy with this charset, just click it in the Status bar, and select the desired one:


[img_assist|nid=2935|title=|desc=|link=none|align=left|width=416|height=274]

 

 

 

 

 

 

 

 

 

 

 

 

  • myXhtmlFile.xhtml and myJsp.jsp have encoding UTF-8, and it is not editable neither in the Status bar nor in the File/Directory Options dialog! What does it mean?

    XHTML and JSP files contain explicit encoding declaration that overrides encoding settings defined on the directory or project levels.

[img_assist|nid=2937|title=|desc=|link=none|align=left|width=408|height=259]

 

 

 

 

 

 

 

 

 

 

 

 

In this case, the only way to change encoding lies with the editor. Delete encoding in the source file, press Ctrl+Space, and enjoy the powerful IntelliJ IDEA’s code completion:

[img_assist|nid=2936|title=|desc=|link=none|align=left|width=461|height=174]

 

 

 

 

 

 

 

 

  • What happens, when a file contains BOM? You cannot change encoding of such file neither in the Status bar or Settings dialog, nor in the editor:

[img_assist|nid=2938|title=|desc=|link=none|align=left|width=266|height=159]

 

 

 

 

 

 

 

 

However, if such file is modified, you can save it in a different encoding. To do that, just open this file for editing and start typing. Encoding icon in the Status bar becomes enabled, and you can easily select the desired charset:

[img_assist|nid=2939|title=|desc=|link=none|align=left|width=329|height=161]

 

 

 

 

 

 

 

 

Enjoy!

 

intellij

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Running Databases on Kubernetes
  • Fixing Bottlenecks in Your Microservices App Flows
  • Microservices 101: Transactional Outbox and Inbox
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: