javafilewriterillegal-characters

FileWritter write integers as illegal characters


i'm currently writting an algorithm who write another algorithm. For this i'm using the FileWritter class and this worked fine until i tried to write integers in the file.

fileWriter.write(1); print `` (edit : you can't see the character in here) that intelliJ interpret as : Illegal Character U+0001 (every number has a different name of illegal character)

When i'm doing fileWriter.write("1") this work fine.

Does the problem come from Intellij or do i have to convert every integers to String ?


Solution

  • Basically Writer.write(int) doesn't do what you expect it to. The documentation says:

    Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

    So you're writing U+0001, just as IntelliJ is saying.

    It's not the greatest API in the world (it would be better to accept char for a single character) but it is behaving as designed.

    Basically, yes, you need to convert everything to text.