Thursday, September 27, 2012

Loading a properties file

It is s not advisable to hard code the values in a java programming.  The efficient way of doing it is by using Properties file , which actually store static information in key and value pair. 

 1.Loading a propertied file present in the System location

import java.io.*;
import java.util.*;
public class ReadPropertiesFile {
       
public static void main(String[] args) {
               
Properties prop = new Properties();
               
try {
                        prop
.load(new FileInputStream("C:/data.properties"));
                       
String name = prop.getProperty("name");
                       
String address = prop.getProperty("address");
                       
System.out.println("Name: " + name);
                       
System.out.println("Address: " + address);
               
} catch (Exception e) {
               
}
       
}
}


2.Loading a properties file present in java package


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
public class LoadPropertiesExample {
  
    private Properties configProp = new Properties();
    public static void main(String[] args) {
       
     InputStream in = this.getClass().
        getResourceAsStream("/com/csc/config/config.properties");
    try {
            configProp.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
      

        System.out.println(configProp.getProperty("name"));
    }
}


Monday, September 24, 2012

Copy File from one directory to other directory




Let us suppose  I have a file  "hi.txt"  in One directory  and I want to copy the fille to other location with the name "bye.txt".
Using java , We can  achieve this functionality by making use of transferring in bytes ....

Following program illustrates how it can be done.



import java.io.*;
public class Main
{
public static void main(String a[])
    {
    try
        {
       // The source location is pointed by  inputstream object "in"
        InputStream in = new FileInputStream("D:\\hi.txt");

       // The destination location is pointed by  outputstream object "obj"
        OutputStream out = new FileOutputStream("E:\\bye.txt");

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    catch (Exception exception)
    {
        System.out.println("Exception "+exception.printStackTrace());
    }
    }
}

Recursive Directory Traversal

package kmr;
 
import java.io.* ;
 public class ReadFoldersRecursively
{
   public void traverse( File file )
   {
    System.out.println( file ) ;
      if( file.isDirectory() )
      {
         String list[] = file.list() ;
         if( list != null )
         {
            for( String item : list )
            {
                traverse( new File(file,item) ) ;
            }
         }
      }
   }

   public static void main( String args[] )
   {
   
      ReadFoldersRecursively rt = new ReadFoldersRecursively() ;
    // Input folder Root folder name
      String folder = "D:\\Hi";
      rt.traverse( new File(folder) ) ;
      
   }
  
   
}