Return file in a directory
String[] list()
- Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
String[] list(FilenameFilter filter)
- Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
File[] listFiles()
- Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
File[] listFiles(FileFilter filter)
- Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
File[] listFiles(FilenameFilter filter)
- Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
import java.io.File;
public class Main {
public static void main(String[] args) {
File[] files = new File("c:/").listFiles();
for(File file: files){
System.out.println(file);
}
}
}
The output:
c:\a.htm
c:\a.xml
c:\abc8669897675133345122txt
c:\aFolder
c:\AUTOEXEC.BAT
c:\boot.ini
c:\CONFIG.SYS
c:\Documents and Settings
c:\IO.SYS
c:\Java_Dev
c:\MSDOS.SYS
...
...
...
The following code implements the FileFilter interface and only accepts the readable files.
import java.io.File;
import java.io.FileFilter;
public class Main {
public static void main(String[] args) {
File[] files = new File("c:/").listFiles(new MyFileFilter());
for(File file: files){
System.out.println(file);
}
}
}
class MyFileFilter implements FileFilter{
public boolean accept(File pathname) {
if(pathname.canRead()){
return true;
}else{
return false;
}
}
}
The output:
c:\a.htm
c:\a.xml
c:\abc8669897675133345122txt
c:\aFolder
c:\ATI
c:\AUTOEXEC.BAT
c:\backup
c:\boot.ini
c:\CONFIG.SYS
...
...
Home
Java Book
File Stream
Java Book
File Stream
File:
- File
- Constants from File class
- Create file object
- Is it executable, readable or writable
- Compare two file path
- Create a file
- Delete a file
- Is it a file or a directory
- Whether the file or directory denoted by this abstract pathname exists
- Whether this abstract pathname is absolute
- Is this file hidden
- Get the file last modified time
- Get the file size
- Get file path and name
- Get free space, total space, usable space
- Get parent file
- Return file in a directory
- The file system roots
- Create new directories
- Rename file
- Change to executable, readable, writable
- Change last-modified time
- Get string representation of a file location
- Convert file location to URI and URL