public synchronized void persistProperties ( ) throws IOException { beginPersistence ( ) ; // Read the properties file into memory // Shouldn't be so bad - it's a small file List < String > content = readFile ( propertiesFile ) ; BufferedWriter bw = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( propertiesFile ) , StandardCharsets . UTF_8 ) ) ; try { for ( String line : content ) { String trimmed = line . trim ( ) ; if ( trimmed . length ( ) == 0 ) { bw . newLine ( ) ; } else { Matcher matcher = PROPERTY_PATTERN . matcher ( trimmed ) ; if ( matcher . matches ( ) ) { final String key = cleanKey ( matcher . group ( 1 ) ) ; if ( toSave . containsKey ( key ) || toSave . containsKey ( key + DISABLE_SUFFIX_KEY ) ) { writeProperty ( bw , key , matcher . group ( 2 ) ) ; toSave . remove ( key ) ; toSave . remove ( key + DISABLE_SUFFIX_KEY ) ; } else if ( trimmed . startsWith ( COMMENT_PREFIX ) ) { // disabled user write ( bw , line , true ) ; } } else { write ( bw , line , true ) ; } } } endPersistence ( bw ) ; } finally { safeClose ( bw ) ; } }
Saves changes in properties file . It reads the property file into memory modifies it and saves it back to the file .