Re: I have something bugs
On Jul 3, 11:12 am, "alex.kor...@gmail.com" <alex.kor...@gmail.com>
wrote:
Hello. I have something bugs, i am dummy in java.
Source:
import java.io.File;
import java.io.FilenameFilter;
public class Main {
public static void main(String[] args) {
File f = new File("C:\\Windows");
String[] strings = f.list( new FilenameFilter() { // throw exception
public boolean accept(File arg0, String arg1) {
if (new File(arg0.getAbsolutePath()).isDirectory())
return true;
return false;
}
});
for (String value : strings) {
System.out.println( value );
}
}
}
simple scan directory. i must printing name of directory.
but this code is printing all files and directories in C:\Windows\
The logic in accept( ) is incorrect. You need to check for the full
file, and not just the parent folder. To get the full file name, you
need to join the directory (arg0) and the file (arg1) with the
separator character.
Change the logic to:
if (new File(arg0.getAbsolutePath()+File.separator
+arg1).isDirectory())
return true;
and it should work.
-cheers,
Manish