String Template="<P>sooper</p>
String InputFolder="D:\\project"
String title="name"
FileWriter myWriter = null;
File htmlContent = new File(InputFolder File.separator title ".html");
myWriter = new FileWriter(htmlContent);
myWriter.write(Template);
myWriter.close();
這作業正常
但是當我用任何包含特殊字符的字串替換標題時,不會創建 html 檔案
我期待一個名為 name?.html 的 html 檔案被創建
uj5u.com熱心網友回復:
您的問題是您嘗試使用對您的作業系統的路徑無效的特殊字符創建路徑(在 Windows 或 Linux 下)。您必須將路徑編碼到正確的路徑,并將非ascii符號替換為其可列印的替代項。
public static void main(String... args) throws IOException {
String template = "<p>sooper</p>";
String parent = "d:/project";
String title = "n ame";
File file = new File(parent, encode(title ".html"));
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
try (FileWriter out = new FileWriter(file, true)) {
out.write(template);
}
}
private static String encode(String str) throws UnsupportedEncodingException {
return URLEncoder.encode(str, StandardCharsets.UTF_8.toString());
}
uj5u.com熱心網友回復:
可能是因為您的 HTML 檔案名中有空格。您可以嘗試使用 Bharatiya_Janta_Party.html 或 Bharatiya-Janta-Party.html
請告知您得到了什么結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526622.html
