Saturday, December 3, 2011

Fibonacci

package au.com.d2klry;

public class Fibonacci {

    public static void main(String[] args) {
        long prevValue = 1;
        long prevPrevValue = 0;
        long currentValue = 0;
        for (int i = 0; i < 20; i++) {

            prevPrevValue = prevValue;
            prevValue = currentValue;

            System.out.format("%d\t", currentValue);
            currentValue = prevValue + prevPrevValue;
        }
    }
}


Monday, November 7, 2011

Iterate over HashMap

package au.com.d2klry.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class HashMapUtil {

    public static void main( String[] args ) {
        HashMapUtil hashMapUtilObj = new HashMapUtil();
        Map<string, string=""> map = hashMapUtilObj.getHashMap();
        Set<string> keySet = map.keySet();
            for(String key : keySet){
                System.out.println("(Key,Value) -> ("+key+","+map.get(key)+")");
            }
  
    }
 
    public Map<string, string=""> getHashMap(){
        Map<string, string=""> map = new HashMap<string, string="">();
        int maxSize = 10;
  
        for (int i = 0; i < maxSize; i++) {
            map.put( "KEY"+(i+1), UUID.randomUUID().toString() );
        }
        return map;
    }
}


Saturday, November 5, 2011

Split a string on commas and whitespaces

package au.com.d2klry.string;

public class Tokenizer {

    public static void main(String args[]){
        String fullNameWithCommaNSpace = "Lastname,  FirstName";
        String fullNameWithComma = "Lastname,FirstName";
        String fullNameWithSpace = "Lastname FirstName";

        String regExp = "[,\\s]+";
    
        for (String token : fullNameWithCommaNSpace.split(regExp))
            System.out.println("FullNameWithCommaNSpace Token : " + token);

        for (String token : fullNameWithComma.split(regExp))
            System.out.println("FullNameWithComma Token : " + token);

        for (String token : fullNameWithSpace.split(regExp))
            System.out.println("FullNameWithSpace Token : " + token);
    }

}

Monday, October 31, 2011

Read and Write the properties file.

package au.com.d2klry.properties;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class ReadPropertiesFile {

    public static void main(String[] args) {
        //First section is creating the properties file
        String fileName = "C:\\temp\\java\\properties.txt";
        String newFileName = "C:\\temp\\java\\newPropertiesFile.txt";
        try {
            File propertiesFile = new File(fileName);
            if(!propertiesFile.exists()){
                propertiesFile.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(propertiesFile));
            writer.write("database_name=DB_TEST");
            writer.newLine();
            writer.write("database_host=localhost");
            writer.newLine();
            writer.write("database_port=50000");
            writer.newLine();
            writer.write("#database_username=scott");//Add a comment to properties file
            writer.newLine();
            writer.write("#database_password=tiger");
            writer.flush();
            writer.close();
            //End of creating properties file
       
            //Second part is reading the properties from the file, printing the properties to the console
            //and writing properties to the new file
            Properties properties = new Properties();
            properties.load(new FileInputStream(fileName));
            System.out.println("database_name -- > " + properties.getProperty("database_name"));
            System.out.println("database_host -- > " + properties.getProperty("database_host"));
            System.out.println("database_port -- > " + properties.getProperty("database_port"));
            //Property value will be null as it is not defined in the original properties file
            System.out.println("database_username -- > " + properties.getProperty("database_username"));
           
            properties.store(new FileOutputStream(newFileName), "File created by d2klry");
           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Writing data to a new file

package au.com.d2klry.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteData {
    public static void main(String args[]){
        try {
            File file = new File("C:\\temp\\java\\WriteData.txt");
            if(!file.exists())
                file.createNewFile();
           
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write("First Line");
            writer.newLine();//Add a new line
            writer.write("Second Line");
            writer.newLine(); //Add a new line
            writer.write("\tThird Line");//Add a tab before the third line content
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Creating a new file using java

package au.com.d2klry.file;

import java.io.File;
import java.io.IOException;

public class CreateFile {
    public static void main(String args[]){
        try {
            File file = new File("C:\\temp\\java\\CreateFile.java");
            if(!file.exists()){
                file.createNewFile();
                System.out.println("Successfully created a new file");
            }
            else{
                System.out.println("File already exists!!!");
            }
           
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Creating a Directory using Java

package au.com.d2klry.file;

import java.io.File;

public class CreateDirectory {
    public static void main(String[] args) {
       
        File directory = new File ("C:\\temp\\java");
        if(!directory.exists()){
            //If temp directory does not exist then java directory won't be created
            if(!directory.mkdir())
                directory.mkdirs();//Creates necessary parent directories.
        }
        else{
            System.out.println("Directory already exists");
        }
    }

}