• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 167
  • Last Modified:

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
0
srikotesh
Asked:
srikotesh
  • 9
  • 8
1 Solution
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now