Java – unable to write Chinese characters to file name

@H_ 301_ 2@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 file names So I can create one named hello Txt file@ H_ 419_ 5@

But I got a FileNotFoundException with the above code, which is completely normal for English characters, but not for Chinese characters@ H_ 419_ 5@

I follow the answer here: how to write a UTF-8 file with Java? Generate the above code, but it doesn't work@ H_ 419_ 5@

Does anyone know how to achieve this goal@ H_ 419_ 5@

Stack trace: @ h_ 419_ 5@ @H_ 301_ 2@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)

Use NiO: @ h_ 419_ 5@ @H_ 301_ 2@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();

Heap: @ h_ 419_ 5@ @H_ 301_ 2@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)

Solution

I found that this problem is related to the character encoding of the eclipse console, not Java

I used the same code and different running configurations, as shown below, @ H_ 419_ 5@

@H_ 419_ 5@

Now after running the program, I get the following output in the console, @ H_ 419_ 5@ @H_ 301_ 2@Exception in thread "main" java. io. FileNotFoundException: C:\temp\??. txt (The filename,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: for the iso-8859-1 encoding in the running configuration, scanner will not be able to correctly read these characters from the console, because the console has different character encoding, will you have? As the file name@ H_ 419_ 5@

Please change the character encoding of the console. I firmly believe that you are using some ides You may have changed or your console inherited character encoding instead of encoding these characters@ H_ 419_ 5@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>