Note that there are some explanatory texts on larger screens.

plurals
  1. POfile.exists() method returns false even if file exists
    text
    copied!<p>I created a program to display all files and sub-directories present in a directory,the directory name being passed as an argument from command line in cmd.<br> The first <strong>if</strong> condition(<strong>if(f.isDirectory())</strong>) checking whether the given argument is directory or not works fine.<br> Then i use File.list() method that is <strong>dir_content = f.list()</strong> to store a list of all filenames in the directory in an array of String dir_content.<br> Then in for loop ,each time a new file object is created for each file name stored in the String array dir_content that is <strong>File f1 = new File(dir_content[i])</strong> and then check for existence of the file is made using exists() method that is <strong>if(f1.exists())</strong>.<br> Now problem is that <strong>f1.exists</strong> returns false for many of files despite of their existence in the given directory.It displays a few files, moreover doesn't show sub-directories.<br> Below is the code of my program:- </p> <pre><code>import java.io.File; class FileDemo1 { public static void main(String[] a) { File f = new File(a[0]); String[] dir_content; if(f.isDirectory()) { System.out.print(f.getName()+" is a directory containing "); dir_content = f.list(); for(int i=0;i&lt;dir_content.length;i++) { File f1 = new File(dir_content[i]); if(f1.exists()) { System.out.print("\n\t: "+f1.getName()); System.out.print(" "+f1.isFile()); System.out.print(" "+f1.isDirectory()); } } } } } </code></pre> <p>Someone please tell me the reason behind the problem and its solution.<br> Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload