how to replace existing properties reading from another file properties using java

i need add I18N to my properties files
i have to add properties to all languages (default all are in english now)

In real time i have to add 1000 propreties to all languages
each language properties file i have to add it manually.

For sample i have written below program
translation.properties file having indonesia language properties
i need to take these properties add it in indonesia.properties file

BELOW IS THE PROGRAM:

package com.org;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;

public class MyPropAllKeys {

	private Properties prop = null;

	public MyPropAllKeys() {

		InputStream is = null;
		String input = "E:/file_list/translation.properties";
		try {
			prop = new Properties();
			is = new FileInputStream(new File(input));
			prop.load(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Set<Object> getAllKeys() {
		Set<Object> keys = prop.keySet();
		return keys;
	}

	public String getPropertyValue(String key) {
		return this.prop.getProperty(key);
	}

	public static void main(String a[]) throws IOException {
		OutputStream output = null;
		Properties prop = null;
		String outputfile = "E:/file_list/indonesia.properties";
		try {

			output = new FileOutputStream(outputfile);
			prop = new Properties();

			MyPropAllKeys mpc = new MyPropAllKeys();
			Set<Object> keys = mpc.getAllKeys();
			for (Object k : keys) {
				String key = (String) k;
				System.out.println(key + ": " + mpc.getPropertyValue(key));
				prop.setProperty(key, mpc.getPropertyValue(key));
			}
			prop.store(output, null);
		} catch (Exception e) {
			System.out.println(e.getStackTrace());
		} finally {
			output.close();

		}
	}
}

Open in new window


From this program i am able to write all the properties from translation.properties to indonesia.properties
Facing problem here as:
1.it is disturbing existing properties
    i mean some of the properties are missed from existing file looks like writing new file.
2.order also jumbled.

can some one suggest how to replace the existing properties values with out disturbing rest of the properties and make the properties file order as it before.
sample.txt
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
For 1. if some properties are missing, can you make sure that you do not have duplicate property keys?

For 2. do not use a Set. Set does not retain the order, use a List instead.
0
srikoteshAuthor Commented:
Hi

1.There is no duplicate key in my preopeties file,it is overwriting rather than updating,PFA sample.txt
2.Set keys = mpc.getAllKeys();

the above line getAllKeys will written set type only.
tried assigning set values to list,still order is jumbled.

Set keys = mpc.getAllKeys();
List<Object> keys1 = new ArrayList<Object>(keys);

Open in new window

0
srikoteshAuthor Commented:
if i want to update only one or two keys i can use below refer code,but my case i have bulk of key to update in propeties file.

https://stackoverflow.com/questions/15337409/updating-property-value-in-properties-file-without-deleting-other-values
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

girionisCommented:
Can you post your translation.properties and indonesian.properties?
0
srikoteshAuthor Commented:
PFA of sample.txt which I posted earlier.
0
girionisCommented:
Well, in this case you cannot do what you ask. All the keys in the translation.properties file have corresponding keys in the indonesia.properties. Trying to add values from translation.properties into indonesia.properties will result in duplicate keys.
0
srikoteshAuthor Commented:
HI

Please refer my above program and output as well

1.keys are duplicated
i am update key value for each key,so obviously same key will be there while updating
earier indonesia properties file is in english values i am replacing with indonesia language.

2.i am able to update my properties key values but
whatever i have written that is doing overwriting code,
here existing keys are missing that is main issue i am struggling.
0
girionisCommented:
Sorry I still don't understand what you're trying to do.

You have an input file: translation.properties

What should the indonesia.properties file be? Can you post a sample?
0
srikoteshAuthor Commented:
indonesia .properties file:
=====================
inf.debitdate=debit date
inf.processed=processed
inf.cancel=cancel
inf.startdate=start date
inf.enddate=end date
inf.billername=biller name
inf.category=category
inf.accountno=account no
inf.fiterval=filter value
inf.paymenttype=payment type
inf.creditdate=credit date
inf.debitdate=debit date
inf.processed=processed
inf.cancel=cancel




translation.prperties file:
=====================

inf.startdate=mulai tanggal
inf.enddate=tanggal akhir
inf.billername=nama biller
inf.category=kategori
inf.accountno=No rekening

THESE ARE THE TWO PROPERTIES FILES indonesia,translation.
now indonesia file have total 15 propeties those are in english.
translation file have 5 properties same keys which are available in indonesia but value will be in indonesia language.
so i want to update these 5 properties  reading from translation properties file and update it in indonesia properties file.

I am expecting my output from indonesia propeties file is:

expected o/p;:

indonesia .properties file:
inf.debitdate=debit date
inf.processed=processed
inf.cancel=cancel
inf.startdate=mulai tanggal
inf.enddate=tanggal akhir
inf.billername=nama biller
inf.category=kategori
inf.accountno=No rekening
inf.fiterval=filter value
inf.paymenttype=payment type
inf.creditdate=credit date
inf.debitdate=debit date
inf.processed=processed
inf.cancel=cancel
0
girionisCommented:
Ok I see what you mean now. Well you cannot do what you're asking with the same file. You will have to load the properties into memory and save them into another file. Also the duplicate properties will be removed since they are invalid in the same file.

Try the following:

package com.org;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;

public class MyPropAllKeys {

	private Properties prop = null;

	public MyPropAllKeys() {

		InputStream is = null;
		String input = "E:/file_list/translation.properties";
		try {
			prop = new Properties();
			is = new FileInputStream(new File(input));
			prop.load(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Set<Object> getAllKeys() {
		Set<Object> keys = prop.keySet();
		return keys;
	}

	public String getPropertyValue(final String key) {
		return this.prop.getProperty(key);
	}

	public static void main(final String a[]) throws IOException {

		InputStream indonesianInputStream = null;
		String indonesianInput = "E:/file_list/indonesia.properties";
		Properties indonesian = new Properties();
		try {
			indonesianInputStream = new FileInputStream(new File(indonesianInput));
			indonesian.load(indonesianInputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			MyPropAllKeys mpc = new MyPropAllKeys();
			Set<Object> keys = mpc.getAllKeys();
			for (Object k : keys) {
				String key = (String) k;
				System.out.println(key + " " + mpc.getPropertyValue(key));
				indonesian.setProperty(key, mpc.getPropertyValue(key));
			}
		} catch (Exception e) {
			System.out.println(e.getStackTrace());
		} finally {
			indonesianInputStream.close();
		}

		OutputStream output = null;
		String outputfile = "E:/file_list/indonesiaNew.properties";
		try {

			output = new FileOutputStream(outputfile);
			indonesian.store(output, null);
		} catch (Exception e) {
			System.out.println(e.getStackTrace());
		} finally {
			output.flush();
			output.close();
		}


	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Hi girionis,

thanks for valuable suggestion,
yes, you are correct after rewriting into another file it is coming as expected.

Is is possible to get the order.i mean same order how the existing indonesia propeties is there it should be like that
here order is jumbled.

sample o/p:
inf.debitdate=debit date
inf.processed=processed
inf.cancel=cancel
inf.startdate=mulai tanggal
inf.enddate=tanggal akhir
inf.billername=nama biller
inf.category=kategori
inf.accountno=No rekening
inf.fiterval=filter value
inf.paymenttype=payment type
inf.creditdate=credit date
0
girionisCommented:
Try to use a TreeSet instead of a Set.
0
srikoteshAuthor Commented:
HI GIRIONIS,

Set keys = mpc.getAllKeys();
the above line getAllKeys will written set type only. I am unable change it.

treeset will give order by keys,but i am expecting exsiting values should be insertion order as it is (List is preferable but not sure where to use).

pls refer my sample o/p on above comment.
0
girionisCommented:
Is there any reason why you need insertion order?
0
srikoteshAuthor Commented:
hi,
I have to do check in, this properties file
 
when I do svn comparison with  previous file I am unable to do compare.
0
girionisCommented:
The problem is that the Set is unordered, so when you get the keys they are unordered. You need to manually order them and then base the for loop on the ordered keys.
0
srikoteshAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.