Wes*_*Gun 7 java ioexception java-11
I want to createNewFile with a path but I got an IOException. The question is, the detailed message cannot be interpreted, I can only see a bunch of question marks.
I am with Windows 10 originally in Spanish, but with Chinese language pack installed. The java language already set to en and file encoding UTF-8:
java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
Run Code Online (Sandbox Code Playgroud)
Why? Only this exception cannot be read.
EDIT: tried to define the language as zh, but does not work.
Use this main class to reproduce it:
public class PromotionTargetFileHandlerMain {
public static final String uploadingDir = String.join(File.separator,
System.getProperty("user.dir"), "targets_csv");
private static final File destDir = new File(uploadingDir);
public static void main(String[] args) {
createFileDestination("target.csv");
}
public static void createFileDestination(String filename) {
if (!destDir.exists()) {
try {
Files.createDirectory(Path.of(uploadingDir));
} catch (FileAlreadyExistsException e) {
log.trace("File dir already exists: {}", uploadingDir);
} catch (IOException e) {
log.error("Cannot create temp file dir {}", uploadingDir, e);
throw new RuntimeException(e);
}
}
String saveLocation = String.join(File.separator,
uploadingDir, filename
);
File saveFile = new File(saveLocation);
if (saveFile.exists()) saveFile.delete();
try {
saveFile.createNewFile(); // <--------------- here IOException
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可能找到一种解决方案:在控制面板中更改 Windows 10 中非 Unicode 应用程序的区域设置。它是西班牙语,但现在我改为中文,然后我可以看到其他 IO 错误的一些本地化消息(无法重现问题中的相同错误):
我找到了这个:
...并且 .NET 异常消息将被本地化,无论消息的预期接收者如何。
所以这让我相信这个过程是这样的:
在 Windows 10 中,您可以在搜索栏中输入“控制面板”,然后转到:区域 -> 管理(选项卡) -> 非 Unicode 应用程序语言,设置为中文(或本地语言包中的其他语言),然后重新开始。
当然,它也会影响其他非 Unicode 应用程序。
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |