- Timestamp:
- 2010-08-11T21:28:34+12:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r20446 r22605 459 459 return new_name; 460 460 } 461 462 463 /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file. 464 * If the file already contains the line as-is, it is not re-written. 465 * If the file doesn't contain the line, it is appended. 466 * If the file contained a different value for the property, the line is corrected 467 * and the file is written out. 468 * Not using the Properties class, as we want to keep the file's contents in the 469 * same order and preserve all the comments in as they're meant to help the user. 470 */ 471 public static void updatePropertyConfigFile(String filename, String propertyName, String propertyValue) { 472 BufferedReader fin = null; 473 BufferedWriter fout = null; 474 StringBuffer contents = new StringBuffer(); 475 String insertLine = propertyName+"\t"+propertyValue+"\n"; // new line after every propertyLine 476 boolean found = false; 477 try { 478 fin = new BufferedReader(new FileReader(filename)); 479 String line = ""; 480 while((line = fin.readLine()) != null) { 481 if(line.startsWith(propertyName)) { // won't match comment 482 found = true; 483 if(line.equals(insertLine)) { // file is already correct, nothing to do 484 fin.close(); 485 fin = null; 486 break; 487 } else { 488 contents.append(insertLine); 489 } 490 491 } else { // any other line 492 contents.append(line); 493 contents.append("\n"); // ensures the required new line at end of file 494 } 495 } 496 497 if(fin != null) { // need to write something out to the file 498 fin.close(); 499 fin = null; 500 501 // if collecthome wasn't already specified in the file, append it 502 if(!found) { 503 fout = new BufferedWriter(new FileWriter(filename, true)); // append mode 504 fout.write(insertLine, 0, insertLine.length()); 505 } else { 506 507 fout = new BufferedWriter(new FileWriter(filename)); // hopefully this will overwrite 508 fout.write(contents.toString(), 0, contents.length()); 509 } 510 511 fout.close(); 512 fout = null; 513 } // else the file is fine 514 } catch(IOException e) { 515 System.err.println("*** Could not update file: " + filename); 516 System.err.println("with the " + propertyName + " property set to " + propertyValue); 517 System.err.println("Exception occurred: " + e.getMessage()); 518 } finally { 519 try { 520 if(fin != null) { 521 fin.close(); 522 fin = null; 523 } 524 if(fout != null) { 525 fout.close(); 526 fout = null; 527 } 528 } catch(IOException ioe) { 529 fin = null; 530 fout = null; 531 } 532 533 } 534 535 } 461 536 462 537 }
Note:
See TracChangeset
for help on using the changeset viewer.