How do I load variables from a file back into Scala?

I have variables in scala that have their values written to an external file in this format:

1
2
0
0.5
0.62

This is my code that puts them in the file called "myVars.txt":

  val pw = new java.io.PrintWriter("myVars.txt")
  pw.println(beginGameCounter)
  pw.println(triangleCount)
  pw.println(triangleScore)
  pw.println(lives)
  pw.println(curPlayer.getX)
  pw.println(curPlayer.getY)
  pw.close

How would I load these variables back into scala and replace the old variables with these new variables (for example the old variable for beginGameCounter is 0, then when I load the file, I want it to become 1)?

Thank you.

Answers


You could try something like this:

  val values = Source.fromFile(pathToFile).getLines.toList
  beginGameCounter = values(0)
  triangleCount = values(1)
  triangleScore = values(2)
  lives = values(3)
  currPlayer.setX(values(4))
  currPlayer.setY(values(5))

This of course assumes that the what variable is on what line is consistent.


Need Your Help

Formatting a JSON object in PHP

php multidimensional-array json

So I have the following two sql queries, and I'm trying to build one JSON object for use in my Angular front end.

How to set the background color for UIWebView

iphone uiwebview background-color

I want to change the background color of the webview. Not for the HTML document it display but for the actual scroll view which can be scrolled. If you scroll a web view you see a gray background c...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.