Tuesday, December 12, 2017

Get The list of all file name from directory/folder and store into string array in java

Get The list of all file name from directory/folder and store into string array in java


import java.io.FileReader ;
    import java.io.File ;
    import java.io.FileNotFoundException ;
    import java.io.IOException ;
    import java.util.ArrayList ;

    class GetFileFromFolder {

        public String[] listOfFiles() {
            String pathLevel = "/home/brij/brijJava";

            File file = new File(pathLevel);
            String[] levelNames = file.list();

            return levelNames;
        }

        public static void main(String arg[]) {

            GetFileFromFolder obj = new GetFileFromFolder();

            String[] allFile = obj.listOfFiles();
            for (int i = 0; i < allFile.length; i++) {
                System.out.println("File Name " + allFile[i]);
            }

        }
    }

Now compile this from command line

 For compile


javac GetFileFromFolder.java

 For execute 
java GetFileFromFolder

File Name GetFileFromFolder.java
File Name GetFileFromFolder.java~
File Name GetFileFromFolder.class
File Name sql
File Name 25-11-17


No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...