Reputation: 221
i am trying to append current date time to filename and pass it to creation of file...so basically this is my code
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 ");
}
}
}
this my code please help me in how to append current date time to file name and create the file with mentioned directory
Upvotes: 0
Views: 2352
Reputation: 121971
Forward slash, /
, and colon, :
, are not permitted characters for a filename on windows. From 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.
The date format string needs to change. For example:
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Upvotes: 6