I have problems with the following code. I am trying to look at an existing directory to see if a file exists before creating a new one, but it doesnt seem to create a new file even though there are no existing ones in the directory. I have attached the two relevant methods, but the problem lies with the writeFile() method. I tried to use the existing ‘dir’ in writeFile, this didnt do the trick either. The rest of the program does seem to work, just the writeFile method has problems.
public void writeFile(String t) throws IOException {
File temp1 = new File(dateNow + File.separator + "Temperature.txt");
boolean check = temp1.exists();
if (!check)
newFiles();
}
public void newFiles() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
dateNow = formatter.format(now.getTime());
System.out.println(dateNow);
// if (hour == 00 && mini == 00 && sec == 00) {
try {
dir = new File(dateNow);
boolean x = dir.mkdir();
// ....
} catch (Exception e) {
//
}
}
you need to specify the full path, not just the directory and the file name to create a file or even check it’s existence,.
thank you for michael667, you remind me about the relative position is right,. 🙂
and there should be no problem with the above code,.