Friday, March 13, 2015

Java useful programs



Get file list from a folder filtered by extensions

package com.java2novice.files;
import java.io.File;
import java.io.FilenameFilter;
 public class MyFilteredFileList
{
    public static void main(String a[])
    {
        File file = new File("C:/MyFolder/");
        File[] files = file.listFiles(new FilenameFilter()
        {          
                   public boolean accept(File dir, String name)
                       {
                if(name.toLowerCase().endsWith(".csv"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        });
        for(File f:files)
        {
            System.out.println(f.getName());
        }
    }
}

Read input from java console in java

package com.java2novice.files;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class ReadFromConsole
{
    public static void main(String a[])
    {
        BufferedReader br = null;
        Reader r = new InputStreamReader(System.in);
        br = new BufferedReader(r);
        String str = null;
        try
            {
            do
               {
                System.out.println("Enter Input, exit to quit.");
                str = br.readLine();
                System.out.println(str);
            }
             while (!str.equalsIgnoreCase("exit"));
           }
            catch (IOException e)
            {
            e.printStackTrace();
        }
          finally
           {
            try
               {
                if(br != null) br.close();
            }
           catch(Exception ex){}
        }
    }
}

Read property file in static context

package com.java2novice.files;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ClassLoaderFileLoading
 {
           private static Properties appProp = null;
     
    static
           {
        try
            {
 InputStream is=ClassLoaderFileLoading.class. getResourceAsStream("/FileName.properties");
            appProp = new Properties();
            appProp.load(is);
        }
        catch(IOException ex)
         {
            ex.printStackTrace();
        }
         catch(Exception ex)
          {
            ex.printStackTrace();
        }
    }
}

Read file content line by line in java

package com.java2novice.files;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadLinesFromFile
{
          public static void main(String a[])
                  {
        BufferedReader br = null;
        String strLine = "";
        try
         {
            br = new BufferedReader( new FileReader("fileName"));
            while( (strLine = br.readLine()) != null)
            {
                System.out.println(strLine);
            }
        }
        catch (FileNotFoundException e)
        {
            System.err.println("Unable to find the file: fileName");
        }
         catch (IOException e)
        {
            System.err.println("Unable to read the file: fileName");
        }
    }
}

No comments:

Post a Comment