Replace value in a file

Ok, I have a .ini file with values, there is a certain section that I need to manipulate the values of in the file.

For instance, I have this block of parameters and values in the file (not there is still text before and after this, I am just focusing on this section)

[BatteryManager]
;    Battery Manager Configuration Section.
;    - StringSetup parameters are:
;        NumStrings,
;        JarsPerString,
;        StringCharger ('0' for Silcon or '1' for Other),
;        BoostVoltageLevel.
;    - Units for temperature will be first 'C' or 'F' found on line.
;      If 'C' or 'F' not specified, current NMC preference is used.
;    - Calibration items are not to be mass configured so not listed.
WebRefresh=1 (0 to 10 Minute(s))
AlarmResetMode=0 (0 to 1)
BatteryChemistry=0 (0 to 1)
StringSetup="1 (1 to 2), 40 (1 to 400), 1, "
BatteryAHCapacity=100 (100 to 2000)
BalanceBoostTime=3 (3 to 10 Second(s))
CellVoltageLimitMinMax=2.150, 2.400 (2.000 to 2.500 V)
PilotTempLimitMax=95.0 F (85.0 to 120.0 F)
AmbientTempLimitMinMax=50.0, 95.0 F (50.0 to 120.0 F)
ResponseDeviationAlarmLevel=20 (10 to 20 %)
DischargeDeviationAlarmLevel=15 (5 to 20 %)
RippleCurrentAlarmLevel=5 (0 to 2000)

Now, I need to go in and change the value for say: AmbientTempLimitMinMax

it has 2 values that need to be changed.

How would you go about opening this file and chaning just the values for that line?

Thank you very much for your time.

LVL 2
k41d3nAsked:
Who is Participating?
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.

petmagdyCommented:
u can use use import java.text.MessageFormat,  make u ini file at this line look like this

AmbientTempLimitMinMax={0},{1} F

now load the file into string for example:
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("inifile"));
           
            //read matrix from file
                byte[] data = new byte[in.available()];
                in.read(data);
                String strData = new String(data);
   
now use something like this:

Object[] arguments = {
     value1,
     value2
 };

where value1 and value2 are sting representation of the float values u want to write into files

 String result = MessageFormat.format(
     strData,     arguments);

now save result back to file

0
k41d3nAuthor Commented:
I guess I am not sure I know what you mean.

I don't see what is happening, and if I save it back to the file, will it overwrite the whole file and just put that value there?

Pardon my ignorance, I'm not familiar with how java handles files and writing to them.
0
petmagdyCommented:
yes u need to replace the whole file, to write back to file do this:

    File fileToWrite = new File( "c:\\test\\initfile.init ); //the file will be created if not exists
      try
    {
      FileOutputStream out = new FileOutputStream(fileToWrite); //the file will be created if not exists else overwrited

      out.write( result.getBytes() );
      out.close();
    }

    catch (Exception ex)
    {
        ex.printStackTrace();      
    }

offcourse assuming their is c:\test directory
0
esorfCommented:
Check out this .ini reading/writing class.  http://www.codeproject.com/useritems/INIFile.asp

string newValue = "min, max F (comment)";

INIFile ini = new INIFile(filename);
ini.setStringPropert("BatteryManager", "AmbientTempLimitMinMax", newValue);
ini.save();
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
esorfCommented:
Thanks for the points, k41d3n!
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.

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.