0

我正在尝试将当前日期时间附加到文件名并将其传递给文件的创建...所以基本上这是我的代码

public class Main {

    //....

    public static void main(String[] args) throws IOException
    {
        DateFormat dt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String d =dt.format(date).toString();
        String fname = "spy1";
        File dir = new File("E:\\");
        File f = new File(dir,fname+d+".txt");
        if(f.createNewFile())
        {
            System.out.println("file creates");
        }
        else
        {
            System.out.println("file not created ");
        }
    }
}

这是我的代码,请帮助我如何将当前日期时间附加到文件名并使用提到的目录创建文件

4

1 回答 1

6

Windows 上的文件名不允许使用正斜杠、/和冒号、 。:命名文件、路径和命名空间

使用当前代码页中的任何字符作为名称,包括 Unicode
字符和扩展字符集中的字符 (128–255),
下列情况除外:

- 以下保留字符:

  +(大于)
  + :(冒号)
  + " (双引号)
  + /(正斜杠)
  + \(反斜杠)
  + | (垂直条或管)
  + ? (问号)
  + *(星号)

- 整数值零,有时称为 ASCII NUL 字符。
- 整数表示在 1 到 31 范围内的字符,
除了允许使用这些字符的备用数据流。
有关文件流的更多信息,请参阅文件流。
- 目标文件系统不允许的任何其他字符。

日期格式字符串需要更改。例如:

DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
于 2013-05-15T15:10:13.907 回答