Cannot write chinese characters to a filename

依然范特西╮ 提交于 2020-01-03 09:07:29

问题


public static void main(String[] args) throws IOException
{
    Scanner in = new Scanner(System.in);
    String fileName = in.nextLine();

    Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("C:/temp/"+fileName+".txt"), "UTF-8"));//Ex thrown
    out.close();
}

I'm trying to create a writer that can handle chinese characters to the file name. So I can create a file called 你好.txt for example.

However I get a FileNotFoundException with the above code, it works perfectly fine for English characters but not with Chinese characters.

I followed the answers here: How to write a UTF-8 file with Java? to produce the above code but it doesn't work.

Anyone know how can I accomplish this?

Stack Trace:

Exception in thread "main" java.io.FileNotFoundException: C:\temp\??.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)

Using NIO:

Path path = Paths.get("C:/temp/"+fileName+".txt");//throws ex
Charset charset = Charset.forName("UTF-8");
Path file = Files.createFile(path);
BufferedWriter  bufferedWriter = Files.newBufferedWriter(file, charset);
bufferedWriter.close();

Stack:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <?> at index 8: C:/temp/?.txt
    at sun.nio.fs.WindowsPathParser.normalize(Unknown Source)
    at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
    at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
    at sun.nio.fs.WindowsPath.parse(Unknown Source)
    at sun.nio.fs.WindowsFileSystem.getPath(Unknown Source)
    at java.nio.file.Paths.get(Unknown Source)

回答1:


I have found out that this problem is related to the character encoding of eclipse console and not related to the Java.

I have used the same code and used Run Configuration differently as shown below,

Now after running the program I got following output in my console,

Exception in thread "main" java.io.FileNotFoundException: C:\temp\??.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:206)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:95)
    at Test.main(Test.java:21)

Conclusion : Here for ISO-8859-1 encoding in run configuration Scanner will not be able to read those character properly from console because console has different character encoding and you will have ?? as a filename.

Please change character encoding for your console, I firmly believe you are using some IDE. May be you have changed or your console inherited character encoding which is not suppose to encode those characters.



来源:https://stackoverflow.com/questions/32195708/cannot-write-chinese-characters-to-a-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!