Java Can't Open a File with Surrogate Unicode Values in the Filename?

前端 未结 4 593
一整个雨季
一整个雨季 2020-12-03 17:54

I\'m dealing with code that does various IO operations with files, and I want to make it able to deal with international filenames. I\'m working on a Mac with Java 1.5, and

4条回答
  •  误落风尘
    2020-12-03 18:43

    It's a bug in the old-skool java File api, maybe just on a mac? Anyway, the new java.nio api works much better. I have several files containing unicode characters and content that failed to load using java.io.File and related classes. After converting all my code to use java.nio.Path EVERYTHING started working. And I replaced org.apache.commons.io.FileUtils (which has the same problem) with java.nio.Files...

    ...and be sure to read and write the content of file using an appropriate charset, for example: Files.readAllLines(myPath, StandardCharsets.UTF_8)

提交回复
热议问题