Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Finding Files in a Specified Directory

Java Example – Finding Files in a Specified Directory

Java Examples Java Examples

The following example demonstrates finding all files starting with the letter 'b' on the C drive:

Main.java File

import

java

.

io

.*;

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

File

dir

=

new

File

(

"

C:

"

)

;

FilenameFilter

filter

=

new

FilenameFilter

(

)

{

public

boolean

accept

(

File

dir

,

String

name

)

{

return

name

.

startsWith

(

"

b

"

)

;

}

}

;

String

[

]

children

=

dir

.

list

(

filter

)

;

if

(

children

==

null

)

{

System

.

out

.

println

(

"

The directory does not exist or it is not a directory

"

)

;

}

else

{

for

(

int

i

=

0

;

i

<

children

.

length

;

i

++

)

{

String

filename

=

children

[

i

]

;

System

.

out

.

println

(

filename

)

;

}

}

}

}

The output of the above code is:



build

build.xml

Java Examples Java Examples