无法读取路径中包含国际化字符的java文件名
如果使用JFileChooser对话框选择同一个文件,则效果良好 路径类似于 C:\テスト\sample.txt 以下代码不起作用无法读取路径中包含国际化字符的java文件名,java,file,ascii,Java,File,Ascii,如果使用JFileChooser对话框选择同一个文件,则效果良好 路径类似于 C:\テスト\sample.txt 以下代码不起作用 String teststring = "C:\\テスト\\sample.txt"; File file = new File(teststring); BufferedReader reader = new BufferedReader(new FileReader(file)); System.out.println(reade
String teststring = "C:\\テスト\\sample.txt";
File file = new File(teststring);
BufferedReader reader = new BufferedReader(new FileReader(file));
System.out.println(reader.readLine());
...
它会因FileNotFoundException而失败。问题很可能是,当Java编译时,它使用的编码与テスト 人物。您可以通过插入
System.out.println(teststring);
可能不会打印出来テスト
默认情况下,编码是平台编码。如果文件保存为UTF-8,则可以使用
javac -encoding UTF-8 YourClass.java
(或使用中的
任务的encoding=“UTF-8”
属性
编辑:
正如@assylias所指出的,反斜杠需要避免!非常感谢您的帮助。除了您的解决方案之外,以下更改已经解决了这个问题
...
File file = new File(new String(teststring.getBytes(),"utf8");
...
您使用的是哪个文件系统?如果它使用JFileChooser,则编写一个程序并输出JFileChooser实例读取的路径,并检查其是否相同。不确定这是否解决了问题,但您需要转义\因此文件名将为
teststring=“C:\\テスト\\sample.txt“
。