有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

filenotfoundexception Java FileOutputStream Windows/Linux

为什么我得到了这个密码:

OutputStream output = new FileOutputStream("res/" + new java.util.Date().toString() +".properties");

我在大学(笔记本电脑-Ubunutu)和在家(台式电脑-windows)都在编写代码。 我将Eclipse项目与github和EGit同步

现在我无法在我的Windows机器上执行这行代码,但它仍在我的笔记本电脑上运行。。完全相同的代码

获取错误: java.io.FileNotFoundException: res\Thu Jan 08 15:54:39 CET 2015.properties (Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch)

和:at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Sourc at java.io.FileOutputStream.<init>(Unknown Source)


共 (3) 个答案

  1. # 1 楼答案

    new java.util.Date().toString()将以以下格式给出日期

    Thu Jan 08 20:51:01 IST 2015
    

    它包含 windows中的名称不允许使用该字符。但是你可以在linux中使用它们

    因此,如果你想在两个平台上使用相同的东西,你必须改变日期的格式,可能是使用SimpleDateFormat或其他东西 e、 g

    SimpleDateFormat sd = new SimpleDateFormat("YY-MM-DD");
    System.out.println(sd.format(new Date()));
    
  2. # 2 楼答案

    Windows不允许文件名中使用冒号“:”

  3. # 3 楼答案

    我认为问题在于:不是Windows中路径名的有效字符

    从MSDN的文章“Naming Files, Paths, and Namespaces”中:

    [...]

    Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    • The following reserved characters:
      • < (less than)
      • > (greater than)
      • : (colon)
      • " (double quote)
      • / (forward slash)
      • \ (backslash)
      • | (vertical bar or pipe)
      • ? (question mark)
      • * (asterisk)

    [...]