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

Uploading data on Remote Server in Java

This blog demonstrate how to upload data from file to remote server/database. Follow the steps as shown. 1. Create new Dynamic Web Project. 2. Now create all the packages as show in the image below. 3. Create controller with name ImportContact.java 4. Create pojo with name Contact.java 5. Create database layer code with name ImportContactBroker.java 6. Create connectivity with name DBConnection.java 7. Create db connectivity setting file as connectionSetting.java 8. Create jsp page with importcontact.jsp 9. Create supporting files like Constant.java and CsvReader.java 10. You can download the war file and import in your workspace. 11. MultipartRequest in Servlet and inside jsp helps to upload file on the remote server. 12. Download sample csv file from here . 13. Create Table with name Contact in Database as given in configuration file. 14.Just compile and run the code. ImportContact.java package com.uploadfile.controller; import java.io.BufferedReader; impo...

JOINs in MySQL

JOIN's is used to query data from two or more than two table based on relationship between certain columns in these tables. Tables in Database are related to each other with some constraints like (Primary Key & Foreign Key) Primary Key :- Column with unique value for each row that is not value matching to each other in the column. Foreign Key :- Two tables are mapped through concept of primary and foreign key, that means primary key of one table is used in other table as foreign key. fig : Primary Key and Foreign Key Example Query to create the above two tables. CREATE TABLE `project_mst` (   `PK_PROJECT_ID` varchar(10) NOT NULL,   `PROJECT_NAME` varchar(100) default NULL,   `PROJECT_DURATION` varchar(50) default NULL,   `FK_MANAGER_ID` varchar(10) default NULL,   PRIMARY KEY  (`PK_PROJECT_ID`) ) CREATE TABLE `manager_mst` (   `PK_MANAGER_ID` varchar(10) NOT NULL,   `MANAGER_...

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....