Click here to watch in Youtube :
Work Folder:
Java Tutorial : Java IO (Java File - How to get a list of file and dir) |
import java.io.File;
/*
* Read List of Files in a Directory
*/
public class FileDemo1
{
public static void main(String[] args)
{
File file = new File("D:/work/");
/*
* Returns an array of strings naming the files and
* directories in the directory denoted by this
* abstract pathname.
*/
String[] strArray = file.list();
for (String fileName : strArray)
{
System.out.println(fileName);
}
}
}
HelloWorld.txt
java.txt
misc
myfile.txt
FileDemo2.javaimport java.io.File;
/*
* Read List of Files in a Directory
*/
public class FileDemo2
{
public static void main(String[] args)
{
File file = new File("D:/work/");
/*
* Returns an array of abstract pathnames denoting
* the files in the directory denoted by this
* abstract pathname.
*/
File[] fileArray = file.listFiles();
for (File file2 : fileArray)
{
System.out.println(file2.getName());
}
}
}
HelloWorld.txt
java.txt
misc
myfile.txt
No comments:
Post a Comment