![](/img/trans.png)
[英]Getting “The filename, directory name, or volume label syntax is incorrect” error while connecting to MySQL (wamp server) from JAVA (eclipse)
[英]Creating a file in Java with a dynamic name, getting syntax is incorrect error
我正在編寫一個簡單的 Java 應用程序,它基本上記錄了串行設備的輸出(有點像 PuttY)。 到目前為止,數據的流式傳輸和顯示正在工作,我正在進入文件創建和程序的編寫部分,我正在測試我閱讀的一些關於創建文件的代碼:
public void createNewFile() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String newFileName = dateFormat.format(System.currentTimeMillis());
try {
File newFile = new File("C:\\Boxtest-%s.txt",newFileName);
boolean fvar = newFile.createNewFile();
if (fvar) {
System.out.println("File created successfully");
updateStatus("File created successfully!");
}
else {
System.out.println("File already present");
updateStatus("File already exists");
}
} catch (IOException e) {
System.out.println("Exception!");
updateStatus(e.getLocalizedMessage());
}
}
當我查看錯誤消息的狀態時,我收到錯誤消息“文件名、目錄名或卷標語法不正確”。
我認為這是由於文件名有一個變量? ("C:\\Boxtest-%s", newFileName) 但是如何讓它在每次啟動按鈕時創建一個新文件並在文件名中使用當前日期/時間以避免覆蓋舊文件?
問題是日期格式中的冒號。 Windows 中的文件名不允許使用它們。
您可以使用 System.currentTimeMillis(); 沒有格式或沒有冒號的格式。
好的,因此有人建議解決此問題的方法如下:
操作系統不允許從文件名中刪除冒號。
下面的新代碼片段:
fullFilePath = String.format("C:\\boxtest\\%s-Boxtest.txt", newFileName); File newFile = new File(fullFilePath);
所以顯然 %s 沒有被 NewFileName 在新文件標注中替換,所以我必須使用格式字符串來完成它,然后在新文件標注中使用完整路徑。
這是現在 owrking。謝謝 Kornejew
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.