[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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.

0
k41d3n
Asked:
k41d3n
  • 2
  • 2
1 Solution
 
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
 
esorfCommented:
Thanks for the points, k41d3n!
0

Featured Post

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.

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