Friday, September 23, 2011

How to share and access propeties file using singleton and java properties

In this example I have given a Idea to share properties file(configuration file) using Singleton class and Java Properties

first we have our properties file say for example data.properties

name = Rajkumar Singh
course = java

now create a singleton class as fallows

DataSingleton.java
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

package example;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class DataSingleton {
static  private DataSingleton instance = null;
public String name;
public String course;
static public DataSingleton getDataSingleton(){
if(instance == null){
instance = new DataSingleton();
}
return instance;
}

private DataSingleton(){
try{
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
Properties prop = new Properties();
prop.load(br);
name = prop.getProperty("name");
course = prop.getProperty("course");
}catch(IOException e){
e.printStackTrace();
}
}
}

now we have our tester class like this


TesterClass.java
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

public class TesterClass {
public static void main(String[] args) {
DataSingleton ds = DataSingleton.getDataSingleton();
System.out.println(ds.name);
System.out.println(ds.course);
}
}


output:
:::::::::::::::::::::::::::::::::::::::::::::::
Rajumar Singh
java
:::::::::::::::::::::::::::::::::::::::::::::::

the example is self explementory.......hope u enjoy the code segment
looking forward to your reply

Sunday, September 4, 2011

New in Java 7

You'll be able to use String values in switch-case statements! It'll support A COMPLETELY NEW BYTE CODE -- Invoke Dynamic -- which might be used, maybe, by some other languages on the platform, some years from now after Java 7 is widely adopted. You'll be able to put UNDERSCORES in numeric literals -- like 1_345_892!!! Excitement is at an all-time-high!!!!! Java 7 has a known runtime optimization bug that is known to do crazy things, including corrupting files. Use the '-XX:-UseLoopPredicate' command line option to disable that feature/bug. Actually, this bug was present in Java 6. But it's disabled by default in Java 6, so hardly anyone used it. But Java 7 enables it by default, so *everyone* has been testing/using it, and it corrupted lots of files