Special characters in filename java

前端 未结 1 454
情深已故
情深已故 2021-01-20 02:09

I\'m trying to write a file with special characters in filename, for examplo \"tééé ê.mp3\", but the filename always stay with \"?\" instead the character \"é\", I tried a s

1条回答
  •  醉话见心
    2021-01-20 02:56

    Try with Files:

    final Path target = Paths.get("tééé ê.mp3");
    
    try (
        final OutputStream out = Files.newOutputStream(target, StandardOpenOption.CREATE_NEW);
    ) {
        // write
    }
    

    Now, if this is a problem that your filesystem does not support such filenames, you will get an InvalidPathException; unlike File, the new API refuses to create file names which may end up being unreadable.

    If this is the case that you can indeed not create the path, well, you'll have to find a way to escape and unescape somewhat; maybe write an alternate name to some sort of database or something.

    Note that InvalidPathException is unchecked; you therefore have to catch this exception explicitly. Also note that you may get this exception if the current character coding used by the JVM is just not suitable to generate the filename.

    0 讨论(0)
提交回复
热议问题