Muhammad Waseem ullah Muhammad Waseem ullah - 1 month ago 5
Java Question

How to open folder and view properties of folder in Java?

Why

File file=new File("folderName");
doesn't work?
folder name and path is valid. Same code is running for files but not for folders.

File file=new File("folderName");
if(file.exists()){
System.out.println("Folder exists");
}
else{
System.out.println("Folder doesn't exists");
}


For folder
file.exists()
always return false.

I can't use other libraries in my Assignment.

Answer

Most likely this is looking for "folderName" in the wrong place:

File file = new File("folderName");

That uses the current working directory, and it might be different than what you assume (depending on your environment, IDE, etc).

You can easily debug the issue using:

System.out.println(file.getAbsolutePath())

By the way, you should simplify if(file.exists() == true) to if(file.exists()).

Comments