我正在尝试将当前日期时间附加到文件名并将其传递给文件创建...所以基本上这是我的代码
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 ");
}
}
}
这个我的代码请帮助我如何将当前日期时间附加到文件名并使用提到的目录创建文件
答案 0 :(得分:6)
正斜杠,/
和冒号:
是Windows上文件名的不允许字符。来自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: + (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.
日期格式字符串需要更改。例如:
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");