-
.exists URGENT
Hi!
I'm trying to create a file before writing/reading to/from it. I always get an error that the package (although it is a file) doesn't exist. My code is as follows:
File myFile = new File(System.getProperty("user.dir") + File.separator
+ "\\output\\Daganalyse.pdf");
File parentDir = myFile.getParentFile();
if(parentDir.isDirectory())
{
parentDir.mkdirs();
}
(it turns red at the 'if')
Please help me!
Thank you!
-
Re: .exists URGENT
What turns 'red at the if'? Nothing else? You must be using a lousy IDE.
kind regards,
Jos
-
Re: .exists URGENT
Netbeans gives error at the 'if':
illegal start of type
package parentDir does not exist
<identifier> expected
';' expected
thanks for the quick response!
-
Re: .exists URGENT
and of course, the correct code is: if (!parentDir.isDirectory()) (and I have the same problem with .exists), but it still turns red
-
Re: .exists URGENT
Are those statements part of a method body?
kind regards,
Jos
-
Re: .exists URGENT
I've already been able to solve the problem by using different coding:
Code:
static String datum = "14-13-2012";
static String pathDir = "H:\\Weekanalyses";
//static String pathFile = "H:\\Weekanalyses\\newfile" + datum +".txt";
static int i =0;
static String pathFile = "H:\\ Leveranciers\\GoedkoopsteLeveranciers-" + i + ".txt";
public static void main(String[] args)
{File f = new File(pathDir);
try{
if(f.exists()==false){
f.mkdir();
System.out.println("Directory Created");
}
else{
System.out.println("Directory is not created");
}
}catch(Exception e){
e.printStackTrace();
}
try {
File file = new File(pathFile);
if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
Thank you for your help!