不能将中文字符写入文件名 [英] Cannot write chinese characters to a filename

查看:407
本文介绍了不能将中文字符写入文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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抛出
out.close();

$ / code>

我正在创建一个可以处理中文字符的文件名称。所以我可以创建一个名为你好.txt 的文件。



但是我得到一个 FileNotFoundException 与上面的代码,它适用于英文字符,但不适用于中文字符。



我遵循这里的答案:如何用Java编写一个UTF-8文件?生产上面的代码,但它不起作用。

任何人都知道我该怎么做到这一点?



堆栈跟踪:

 线程main中的异常java.io.FileNotFoundException:C:\temp\\\.txt (文件名,目录名称或卷标语法不正确)
在java.io.FileOutputStream.open0(本地方法)
在java.io.FileOutputStream.open(未知源)
< init>(Unknown Source)
在java.io.FileOutputStream。< init>(Unknown Source)
b
$ b

使用NIO:

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

堆叠:

 线程main中的异常java.nio.file.InvalidPathException:Illegal char<?>在索引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来源)
在java.nio.file.Paths.get(未知来源)


解决方案

我发现这个问题与eclipse控制台的字符编码有关,与 Java 没有关系。



我使用了相同的代码,并使用运行配置,如下所示,





现在运行程序后,在线程main中的异常java.io.FileNotFoundException:C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\名称或卷标语法不正确)
在java.io.FileOutputStream.open(本地方法)
在java.io.FileOutputStream。< init>(FileOutputStream.java:206)$ b $ (FileOutputStream.java:95)
at Test.main(Test.java:21)

结论 :此处为 ISO-8859-1 编码在运行配置 Scanner 中将无法读取这些字符p因为控制台具有不同的字符编码,所以你将有 ?? 作为文件名

请更改您的控制台的字符编码,我坚信您正在使用一些IDE。可能是你已经改变了,或者你的控制台继承了字符编码,而不是对这些字符进行编码。


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)

解决方案

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.

这篇关于不能将中文字符写入文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆