Replace value in a file

Posted on 2004-12-01
Last Modified: 2010-03-31
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)

;    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.

Question by:k41d3n
    LVL 13

    Expert Comment

    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()];
                    String strData = new String(data);
    now use something like this:

    Object[] arguments = {

    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

    LVL 2

    Author Comment

    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.
    LVL 13

    Expert Comment

    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
          FileOutputStream out = new FileOutputStream(fileToWrite); //the file will be created if not exists else overwrited

          out.write( result.getBytes() );

        catch (Exception ex)

    offcourse assuming their is c:\test directory
    LVL 3

    Accepted Solution

    Check out this .ini reading/writing class.

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

    INIFile ini = new INIFile(filename);
    ini.setStringPropert("BatteryManager", "AmbientTempLimitMinMax", newValue);;
    LVL 3

    Expert Comment

    Thanks for the points, k41d3n!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now