Reputation: 404
In the code below I tried to set a method which should create files in a directory.
There are two different methods which I tried, but the files seems not to be created.
Perhaps there are some syntax problem?
public void makeNewFiles() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss dd-MM-yyyy");
String ns = sdf.format(d);
File ntf = File.createTempFile(ns, ".png", directory);
}
public void makeNewFiles() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss dd-MM-yyyy");
String ns = sdf.format(d);
File n1 = new File(directory, pathToActualFile);
File n2;
if(n1.exists()) {
n2 = new File(directory, ns + ".png");
n2.createNewFile();
}
}
Upvotes: 3
Views: 557
Reputation: 140465
Depending on your OS,
"hh:mm:ss dd-MM-yyyy"
might or might not be a valid filename (I would simply avoid that space resp. the : colons in there; that could give you trouble in many environments). To be precise: most modern OSes accept spaces in file names, but especially any Unix like file system requires special thought when making command line calls that have to deal with "spacy" filenames. Whereas the colon : is more of a no-go; at least for Windows and Unix like OSes.
Then: if your code is called multiple times during the same second; your file name are still "not good enough" to guarantee that you don't re-create the same file again.
Finally: consider adding some kind of "header" to your string; like
ns = "whatever-" + sdf.format("hh_mm_ss_dd-MM-yyyy")
Upvotes: 2