Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

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.
0
sangameshrh
Asked:
sangameshrh
  • 6
  • 5
  • 2
  • +1
3 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now