0

I learnt that, Java allows file names to have unicode characters.

How to name a file, naïve.java, using english keyboard?

Is there a notation similar to unicode escape notation(used in java source code) that we can use to name java files with unicode characters?

uraimo
  • 19,081
  • 8
  • 48
  • 55
overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

1

All String objects in Java contain UTF-16 Unicode. All Java objects that open files eventually name those files strings.

However, your keyboard is not Java's problem, it's your operating system's problem.

So:

File foo = new File("n\u00e4ive.java");

You edited the question to say that what you really want is to have a Java source file with interesting characters. That's a matter for your favorite text editor and operating system.

Java is perfectly happy to compile files with arbitrary names. However, creating those files and managing those files is not its problem. How you would go about creating such a file is between you and your operating system. Windows, Linux, OSX: all have different tools for entering Unicode characters that aren't part of the obvious keyboard map.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
1

It seems that you are referring to JLS §7.2 “Host Support for Packages”:

A package name component or class name might contain a character that cannot correctly appear in a host file system's ordinary directory name, such as a Unicode character on a system that allows only ASCII characters in file names. As a convention, the character can be escaped by using, say, the @ character followed by four hexadecimal digits giving the numeric value of the character, as in the \uxxxx escape (§3.3).

Under this convention, the package name:

children.activities.crafts.papierM\u00e2ch\u00e9

which can also be written using full Unicode as:

children.activities.crafts.papierMâché

might be mapped to the directory name:

children/activities/crafts/papierM@00e2ch@00e9

Note that this is described as a convention and only for characters not being supported by the host’s filesystem. So the first obstacle is to find a system without Unicode support in the file system API before you can check whether javac still adheres to this convention.

So for most systems, if you want to name a file naïve.java, there’s not only support to name it directly that way, you also have to name it that way as the fallback escaping scheme is not supported by tools designed to run only on systems which don’t need it.

That leads to your other question about how to enter it via the keyboard. Well, that’s system dependent. The most portable solution is:

  • open your browser
  • navigate to this question and mark naïve.java with the mouse
  • press ctrl+c
  • use your favorite tool to create a new .java file
  • when asked for the new name, press ctrl+v

As a general solution, refrain from using every feature, the Java programming language offers…

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765