Appending some values at the top of a File in JAVA

I have text file which looks like :

name=Sandeep
place=Bangalore
age=23
Desig=Designation

Here I want to set My age as 25.

Thanks.
sangameshrhAsked:
Who is Participating?
 
CEHJCommented:
Use a Properties class with a propoerties file
0
 
sangameshrhAuthor Commented:
It will replace the existing contents. I need the existing contents as it is and only a single line to be updated.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
zzynxSoftware engineerCommented:
As far as I know, you can't just replace one line without having to rewrite the whole file
0
 
sciuriwareCommented:
You can open the file as RandomAccessFile, .seek() to the position of those character "3",
and write a "5".

But, how do you know the exact position of the former "3"?
May be by reading from the top till you see the "3", but be careful!

;JOOP!
0
 
CEHJCommented:
If you want to 'save' permanently you must rewrite the file. You can change any number of properties
0
 
CEHJCommented:
>>It will replace the existing contents.

It won't
0
 
sangameshrhAuthor Commented:
My code looks like this

Properties properties = new Properties();
                        try
                        {
                              properties.setProperty("age","28");
                              properties.store(new FileOutputStream(outFileName),null);
                        }
                        catch (IOException e)
                        {
                        }
0
 
CEHJCommented:
You have to load the Properties first. See the example i posted
0
 
zzynxSoftware engineerCommented:
Oh, I see.

with
>> Properties properties = new Properties();
you just create an empty file (in memory)

with
>> properties.setProperty("age","28");
you add the line
age=28

with
>> properties.store(new FileOutputStream(outFileName),null);
You write it to disk (overwriting the original one)

So, like CEHJ told you, you have to load your existing file first, then add the line and then write it again.
0
 
zzynxSoftware engineerCommented:
So to be undoubtfully clear:

Properties properties = new Properties();
try {
       properties.load(new FileInputStream(outFileName));
       properties.setProperty("age","28");
       properties.store(new FileOutputStream(outFileName),null);
} catch (IOException e) {
}
0
 
zzynxSoftware engineerCommented:
Of course, under the hood, the whole file is rewritten.
0
 
CEHJCommented:
Keep a reference to the InputStream as it's *not* closed by Properties
0
 
zzynxSoftware engineerCommented:
CEHJ, thanks for pointing out.
So:

Properties properties = new Properties();
try {
       FileInputStream fis = new FileInputStream(outFileName);
       properties.load(fis);
       properties.setProperty("age","28");
       FileOutputStream fos = new FileOutputStream(outFileName);
       properties.store(fos, null);
       fis.close();
       fos.close();
} catch (IOException e) {
}
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.

All Courses

From novice to tech pro — start learning today.