Showing posts with label Java Program. Show all posts
Showing posts with label Java Program. Show all posts

Saturday, January 20, 2018

Get The html file list using the FileFilter In java



 The following java program get the list of all html file from the directory folder



import java.io.File;
import java.io.FileFilter;


class GetHtmlFilesList {
    FileFilter htmFileFilter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.getName().endsWith(".htm")) return true;
            else return false;
        }
    };
    public File[] getListOfHtmFiles(String dataPath) {
        File dataFolder = new File(dataPath);
        return dataFolder.listFiles(htmFileFilter);
    }
  public static void main(String arg[]) {
        String DATA_PATH="/home/brij/mathmuni2/docs/2017-11-30_updated/htm_and_images";
        GetHtmlFilesList getHtmlFileList=new GetHtmlFilesList();
        File[] fileList=getHtmlFileList.getListOfHtmFiles(DATA_PATH);
       
        for (File f : fileList) {
            System.out.println("File Name = "+f.getName());
        }
  }
}

Wednesday, December 13, 2017

Convert Time In String To Second In java

 In this java program calculate the second from given time in string. For example time="00:2:30:22" so the second of given time is 150 sec.


class ConvertTimeToSecond {

    public int getSecond() {
       //Time is string
        String timestampStr = "00:01:40.70";
        timestampStr = timestampStr.substring(0, timestampStr.lastIndexOf("."));
        String[] tokens = timestampStr.split(":");
        int hours = Integer.parseInt(tokens[0]);
        int minutes = Integer.parseInt(tokens[1]);
        int seconds = Integer.parseInt(tokens[2]);
        int duration = 3600 * hours + 60 * minutes + seconds;
        //System.out.println(duration);

        return duration;
    }

    public static void main(String arg[]) {

        ConvertTimeToSecond obj = new ConvertTimeToSecond();

        int totalSecond = obj.getSecond();

        System.out.println("Total Second " + totalSecond);

    }
}

The output is :
javac ConvertTimeToSecond.java
 java ConvertTimeToSecond

Total Second 100

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


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...