0

I want to create a properties file in my scala and also i want to update it dynamically. I am new so please help me. Give me some idea to achieve this in scala. Thanks in advance

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36

1 Answers1

1

Here is the working solution

val pattern = "#Key {0} Value= {1} "
val key = "your_key"
val value = "your_value"

val result = s"#Key ${key} Value=${value}"

val fw = new FileWriter("message_scala.properties", append = true)
try {
  fw.write(result + "\n")
} finally
  fw.close()
}

To read the property file now

val fileInput = new FileInputStream("message_scala.properties")
val properties = new Properties()
properties.load(fileInput)
println("Key value="+properties.getProperty("your_key"))
Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
  • U have a nice question. Keep posting. keep helping. – Ashish Ratan Jan 20 '14 at 15:07
  • It is fun to see that question asked by Ashish was answered by Ashish :-) . Not a nice question, though -- there is not much scala specific and ordinary java solution (I guess you [have adapted your code from java snippet](https://stackoverflow.com/revisions/21237300/1)) will work fine. – om-nom-nom Jan 20 '14 at 15:23
  • Dude, its not about good or bad, its about to help, Think about the day when You asked your first question on Stack overflow.. i hope you'll understand what i want to say. and yeah its a please to answer Ashish, by Ashish(Me). – Ashish Ratan Jan 20 '14 at 16:02
  • @om-nom-nom I am new on stackoverflow. what was the wrong in my Question? i know it was not valuable for you, but this answers matters for me so much, as i was not having any idea about this problem before. –  Jan 20 '14 at 16:09
  • @AyushmanAshish If you're talking about voting, I'm not saying your question is not interesting **for me** and thus should be closed. What I trying to say is: if you are asking about **property file**, you probably know what it is and likely have seen [this one](http://stackoverflow.com/a/1318391/298389), or similar examples. You're asking about working with property files in **scala**, then you probably know basic Scala syntax. Now, the only thing that is left -- connect two dots. If you already tried to adapt that code, but failed -- we can't help you without you saying this fact to us. – om-nom-nom Jan 20 '14 at 16:25
  • @AshishRatan so why do you think I'm trying to insult somebody and not trying to help, just like you? [The question fits poorly to SO](http://meta.stackexchange.com/a/186575/167396), I have noticed this fact so next time OP will ask the question he will do a better job. It's a win-win situation for both -- answerers and questioner - we will get a better question, he will get a quicker answer. – om-nom-nom Jan 20 '14 at 16:28
  • Ok thanks buddy. I am also beginner. Even i too don't know how to upload image on stackoverflow to embedding with question. :( – Ashish Ratan Jan 20 '14 at 16:28