k41d3n
asked on
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.1 50, 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)
ResponseDeviationAlarmLeve l=20 (10 to 20 %)
DischargeDeviationAlarmLev el=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.
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.1
PilotTempLimitMax=95.0 F (85.0 to 120.0 F)
AmbientTempLimitMinMax=50.
ResponseDeviationAlarmLeve
DischargeDeviationAlarmLev
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.
ASKER
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.
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.
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(fileToWri te); //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
File fileToWrite = new File( "c:\\test\\initfile.init ); //the file will be created if not exists
try
{
FileOutputStream out = new FileOutputStream(fileToWri
out.write( result.getBytes() );
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
offcourse assuming their is c:\test directory
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the points, k41d3n!
AmbientTempLimitMinMax={0}
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