Reading and Writing Properties file Java
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class App
{
@SuppressWarnings("rawtypes")
public static void main( String[] args )
{
Properties prop = new Properties();
try {
// Loading Property file
// This method to load file in Simple Java Application.
prop.load(new FileInputStream("messages.properties"));
// This method to load file when developing web application
//prop.load(this.getClass().getClassLoader().getResourceAsStream("messages.properties"));
// Start ::
//Writing Content to Property file
prop.setProperty("ID121", "Angad Yadav");
prop.setProperty("ID122", "Manish Sharma");
prop.setProperty("ID123", "Sunil Sharma");
// End ::
// Start ::
// Reading Content from Property file
Set set = prop.entrySet();
Iterator it = set.iterator();
while (it.hasNext())
{
Map.Entry me = (Map.Entry) it.next();
System.out.println("Key====>"+me.getKey() +"===Value===>"+me.getValue());
}
//End ::
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Save this messages.properties file in your project or create new .properties file
1. messages.properties
NotEmpty.validationForm.userName=User Name must not be blank.
Size.validationForm.userName=User Name must between 1 to 20 characters.
NotEmpty.loginForm.userName=must not be blank.
Size.loginForm.userName=size must between 1 to 50 characters.
NotEmpty.loginForm.password=must not be blank.
Size.loginForm.password=size must between 1 to 20 characters.
NotEmpty.registration.password=Password must not be blank.
Size.registration.password=Password must between 4 to 20 characters.
message = HelloWorld
Comments
Post a Comment