Pages

Saturday 22 February 2014

How to find out number of folders in directory using Java

SIMPLE JAVA PROGRAM TO FIND OUT NUMBER OF FOLDERS IN DIRECTORY

CODE :


import java.io.File;

public class NumberOfFileInFolder {
          public static void main(String[] args) {
File f1 = new File("D:/SONGS");
                int count1 = 0;
                for (File file : f1.listFiles()) {
                   if (file.isDirectory()) {
                        count1++;
                   }
               }
               System.out.println("Number of directories: " + count1); // Show number of directory only
}
}

How to find out no of files in directory using Java code

SIMPLE JAVA PROGRAM TO KNOW NUMBER OF FILES IN DIRECTORY

CODE :

import java.io.*;
 
public class CountFilesInDirectory {
        public static void main(String[] args) {
                File f = new File("D:/SONGS");
                int count = 0;
                for (File file : f.listFiles()) {
                        if (file.isFile()) {
                                count++;
                        }
                }
                System.out.println("Number of files: " + count);
                // Also do it with bellow one line code...
                System.out.println(new File("D:/SONGS").listFiles().length);
        }
}

Wednesday 19 February 2014

Java Program To Shutdown Computer

This is a simple Java program for shutdown the computer.

This program is used when you make a application that will run for an hour and you don't wait to shutdown your computer, just add this code at last of your application. Your computer will automatically shutdown when your application is over. You can use it more place also.


Here is the code :
------------------------------------------------------------------------------------------------------------
public class ShutdownComputer {

    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Process proc = runtime.exec("shutdown -s -t 30"); // after 30 second it will shoutdown
        System.exit(0);

    }
}
--------------------------------------------------------------------------------------------------------------

COMMENT IF YOU HAVE ANY COMPUTER TRICKS.