gpt4 book ai didi

java - 附加日期时间并将其传递给创建文件

转载 作者:行者123 更新时间:2023-12-01 08:12:03 24 4
gpt4 key购买 nike

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

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

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

最佳答案

正斜杠、/ 和冒号、: 不允许作为 Windows 上的文件名字符。来自 Naming Files, Paths, and Namespaces

Use any character in the current code page for a name, including Unicodecharacters and characters in the extended character set (128–255),except for the following:- The following reserved characters:  +  (greater than)  + : (colon)  + " (double quote)  + / (forward slash)  + \ (backslash)  + | (vertical bar or pipe)  + ? (question mark)  + * (asterisk)- Integer value zero, sometimes referred to as the ASCII NUL character.- Characters whose integer representations are in the range from 1 through 31,except for alternate data streams where these characters are allowed.For more information about file streams, see File Streams.- Any other character that the target file system does not allow.

The date format string needs to change. For example:

DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

关于java - 附加日期时间并将其传递给创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16568706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com