Posts

Showing posts from June 29, 2010

How to calculate number of files in a directory in java code examples

private static int calNoOfFiles(String directoryPath)
    {
        String fileDelimiter= File.separator;
        File fd = new File(directoryPath);
        if(!fd.isFile())
        {
            String list[] = fd.list();
            for(int i = 0; i < list.length; i++)
            {
                String tempDirPath = directoryPath + fileDelimiter + list[i];
                debug(tempDirPath);
                File ftd = new File(tempDirPath);
                if(ftd.isFile())
                    noOfFiles++;
                if(ftd.isDirectory())
                    calNoOfFiles(tempDirPath);
            }

        }
        return noOfFiles;
    }

How to get file extension in java code example

Get file extension in java code example

public static String getFileExtension(String filePath)
    {
        File f = new File(filePath);
        String name = f.getName();
        int k = name.lastIndexOf(".");
        String ext = null;
        if(k != -1)
            ext = name.substring(k + 1, name.length());
        return ext;
    }

What is inner class in java code example

public class Test {

        public void MethodOne() {
                System.out.println("Inside Test class");
        }

        public class InnerC {
            public void MethodOne() {
                System.out.println("Inside InnerC class");
            }

            public void test() {
                this.MethodOne();
                Test.this.MethodOne();
            }
        }

        public static void main(String[] args) {       
                Test.InnerC i = new Test().new InnerC();
                i.test();
        }

}

What are Boxing, Autoboxing and Auto-Unboxing in java example

Boxing :- Convert primitive data to its wrapper class object.
   Example :- char to Charactor
              int to Integer
              boolean to Boolean
              float to Float
          double to Double;

Unboxing :- Java primitive type  to its wrappers class:
   Example :-     Boolean to boolean
         Byte to byte,
        Character to char,
                Double to double,
                Float to float,
        Long to long,
                Integer to int,


public abstract class Test {

    public static void main(String[] args) throws Exception {

        boolean b = true;
        //Explicit Boxing
        Boolean b1 = new Boolean(b);
        //Explicit Unboxing
        boolean b2 = b1.booleanValue(); 

        // Automatic Boxing
        b1 = b; 
        //Automatic Unboxing
        b2 = b1;


        int i = 50;
        //    Explicit Boxing
        Integer i1 = new Integer(i); 
        //    Explicit Unboxing
        int j = i1.intValue();       

        // Auto…

What is the use of startsWith and endsWith method in String java example

// What is the use of startsWith and endsWith method in String java example

public class Test {
        public static void main(String[] args) {       
            String st = new String("test string test here");

            System.out.println(st.startsWith("test"));
            System.out.println(st.startsWith("test", 12));

            System.out.println(st.endsWith("here"));

        }
}

Recent Post

Recent Posts Widget