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\
Oliver Wong - 03 Jul 2007 19:19 GMT
> Hello. I have something bugs, i am dummy in java.
>
[quoted text clipped - 21 lines]
> simple scan directory. i must printing name of directory.
> but this code is printing all files and directories in C:\Windows\
Read the javadocs for FilenameFilter:
http://java.sun.com/javase/6/docs/api/java/io/FilenameFilter.html
- Oliver
alex.korsak@gmail.com - 03 Jul 2007 19:23 GMT
> - Oliver
Ups)) thx.
I am dummies ))
thx, thx
alex.korsak@gmail.com - 03 Jul 2007 19:23 GMT
> - Oliver
Ups)) thx.
I am dummies ))
thx, thx
alex.korsak@gmail.com - 03 Jul 2007 19:25 GMT
> - Oliver
Ups)) thx.
I am dummies ))
thx, thx
alex.korsak@gmail.com - 03 Jul 2007 19:27 GMT
> - Oliver
Ups)) thx.
I am dummies ))
thx, thx
kaldrenon - 03 Jul 2007 19:23 GMT
You want it to print all directories and skip files? I would do it
this way:
File f = new File("C\\Windows");
File[] allItems = f.listFiles();
for(File cur : allItems){
if(cur.isDirectory()){
System.out.println(cur.toString();
}
}
-Andrew
Manish Pandit - 03 Jul 2007 19:24 GMT
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.
>
[quoted text clipped - 22 lines]
> 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
Lars Enderin - 03 Jul 2007 19:32 GMT
Manish Pandit skrev:
> On Jul 3, 11:12 am, "alex.kor...@gmail.com" <alex.kor...@gmail.com>
> wrote:
[quoted text clipped - 35 lines]
> +arg1).isDirectory())
> return true;
You don't need to construct the full file path explicitly if you use the
File(String, String) constructor.
Roedy Green - 03 Jul 2007 22:05 GMT
On Tue, 03 Jul 2007 11:12:34 -0700, "alex.korsak@gmail.com"
<alex.korsak@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>public class Main {
> public static void main(String[] args) {
[quoted text clipped - 12 lines]
> }
>}
Let's tidy it up:
public class TestFilters {
public static void main(String[] args) {
File winDir = new File("C:\\Windows");
String[] subDirNames = winDir.list( new FilenameFilter() {
// accept dirs only.
public boolean accept(File dir, String filename) {
{
return new File( dir, filename).isDirectory();
}
});
for (String subDirName: subDirNames)
{
System.out.println( subDirName);
}
}
The main thing I did was give each variable a clear name as to what it
contained. Without that, programs become needlessly confusing.
The other thing I did was get rid of your "absolute" call. You only
need that when you want the precise file names. They are not needed
to determine if a file is a directory.
Further you the file you are testing has two parts the dir and the
filename. You need to create a file handle to test that includes both
parts.
I am impressed you mastered the delicate syntax of the inner anonymous
class. You are no dummy, just one who has not yet learned the
importance of naming.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com