I have something bugs

A

alex.korsak

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\
 
O

Oliver Wong

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\

Read the javadocs for FilenameFilter:

http://java.sun.com/javase/6/docs/api/java/io/FilenameFilter.html

- Oliver
 
K

kaldrenon

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
 
M

Manish Pandit

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
 
L

Lars Enderin

Manish Pandit skrev:
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;

You don't need to construct the full file path explicitly if you use the
File(String, String) constructor.
 
R

Roedy Green

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 );
}
}
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top