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)
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);
}
}
}