Problem : How you can display list of file names for a given directory?
Solution : java.io.File class has a method listFiles() to get list of file and sub-directories java.io.File objects. Program :Step 1 - CodingCreate a text file c:/sunilos/DirFolder.java and copy below contents.import java.io.File; /** * A program to display files from a folder. */ public class DirFolder { public static void main(String[] args) { File directoy = new File("c:/sunilos"); File[] list = directoy.listFiles(); for(int i = 0; i<list.length;i++){ if(list[i].isFile()){ System.out.println((i+1) + ": " +list[i].getName()); } } } } Step 2 - Deployment
Step 3 - Testing
Output1: sunilos.txt2: sunilos-write.txt 3: sunilos-copy.txt <<Previous | Next>> |