Created
November 9, 2009 05:10
-
-
Save mpen/229709 to your computer and use it in GitHub Desktop.
java.util.Properties load/store, get/set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// java.util.Properties load/store, get/set | |
import java.io._ | |
import java.util.{Date, Properties} | |
import org.apache.commons.io.IOUtils | |
object PropertiesLoadStore { | |
def main(args: Array[String]) { | |
val configFile = new File("PropertiesLoadStore.properties") | |
val config = new Properties | |
// load | |
if (configFile.exists) { | |
var in: InputStream = null | |
try { | |
in = new FileInputStream(configFile) | |
config.loadFromXML(in) | |
} catch { | |
case e: IOException => e.printStackTrace | |
} finally { | |
IOUtils.closeQuietly(in) | |
} | |
} | |
// get | |
println(config.getProperty("言語")) | |
println(config.getProperty("updatedAt")) | |
// set | |
config.setProperty("言語", "日本語") | |
config.setProperty("updatedAt", new Date().toString) | |
// store | |
var out: OutputStream = null | |
try { | |
out = new FileOutputStream(configFile) | |
config.storeToXML(out, "Properties test") | |
} catch { | |
case e: IOException => e.printStackTrace | |
} finally { | |
IOUtils.closeQuietly(out) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment