Wednesday, 21 January 2015

Java-Fetch InternetExplorer Version

Java Program to fetch internet Explorer version.

import java.io.IOException;
public class Test {

    public static void main (String[] args) throws IOException {
        String IEVersioncmd = "reg query \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Internet Explorer\" /v svcVersion";
        System.out.println(execCmd(IEVersioncmd).split("REG_SZ")[1].trim());
     }
   
    @SuppressWarnings("resource")
    public static String execCmd(String cmd) throws java.io.IOException {
        java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }   
}

Executing the above java program:

Output:
Note:
Verified the InternetExplorer version using above program on Windows OS for IE 10 and IE 11.

Internet Explorer Version in IE Browser.

Tuesday, 20 January 2015

Java-Print all the file names starting with "Test-"

Java program to print all the file names starting with "Test-".

package fileio;
import java.io.File;
import java.io.FilenameFilter;

public class FileNames {
   
    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
           
            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : "+foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {
              System.out.println(file.getName());
            }             
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   

}


Executing the above Java Program:

Output:

Java-Delete files for the file names starting with "Test-"

Java Program to delete files for the file names starting with "Test-"

package fileio;
import java.io.File;
import java.io.FilenameFilter;

public class DeleteFileName {

    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir") + "\\src\\fileio";

            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : " + foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {

                try {
                    if (file.delete()) {
                        System.out.println(file.getName() + " is deleted!");
                    } else {
                        System.out.println("Delete operation is failed, unable to delete the file.");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Executing the above java program:

Output:



Java-Print the file content for the file names starting with "Test-"

Java Program to print the file content for the file names starting with "Test-".

package fileio;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

public class PrintFileContent {
   
    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
           
            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : "+foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {
              System.out.println("----------------------------------------------");
              System.out.println("               "+file.getName());
           
              //Prints the file content.
              BufferedReader br = null;
                try {
                    String sCurrentLine;
                    br = new BufferedReader(new FileReader(file));
       
                    while ((sCurrentLine = br.readLine()) != null) {
                        System.out.println(sCurrentLine);
                    }
       
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (br != null)br.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }               
            }             
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

Note:
File "Test-001.txt","Test-002.txt" and "Test-003.txt" should have some content
    Ex: "This content of test file for Test001"









Executing the above java program:


Output: