Skip to main content

Reading and Writing Properties file in Java

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

******* Result******

Comments

Popular posts from this blog

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.

Executing executable Jar file from Java Program

Sample code to execute jar file from Java Program. Pre-requisite 1. Need to have executable jar file. 2. Copy the code in your workspace and Run the code. package com.test.reusable.enterprise.service; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.lang.exception.ExceptionUtils; public class ExecuteJarFile { public static void main(String[] args) { try { //Method1 to Execute Jar file From Java Program ProcessBuilder pb = new ProcessBuilder("java", "-jar", "HelloWorld.jar"); // command to execute jar file with fileName.jar pb.redirectErrorStream(true); pb.directory(new File("C:/www/")); // Directory path where your jar file is placed. Process p = pb.start();

JSON parsing in JAVA

JSON parsing in JAVA package jsonsample; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class JsonSample { public static void main(String[] args) { String str = "{'status': 'ok','found': true," + "'header': {'queried_sn': 'T0650GW872832', 'mfpro_env': 'deere_staging', 'elapsed_seconds': 2.314,'timestamp': '2012-06-14 07:12:18'}," + "'machine': { 'mfpro_machine': {'updated_at': '2012-05-22T10:23:54-04:00','matches': 3,'hours': 6809,'id': 1140530,'features': " + "[{'key': 'pat','label': 'Power-Angle-Tilt','feature_key': 'cons_blades'}]}," + "'make': {'name': 'John Deere','id': 100,'pi': null}," + "