8.42. Scala File I/O

发布时间 :2023-11-21 23:00:02 UTC      

Scala performs file writing operations, directly using the I/O class in java ( java.io.File ):

8.42.1. Example #

import java.io.\_
object Test {
   def main(args: Array[String]) {
      val writer = new PrintWriter(new File("test.txt" ))
      writer.write("Novice Tutorial")
      writer.close()
   }
}

Execute the above code and one will be produced in your current directory test.txt file, the content of which is “Rookie course”:

$ scalac Test.scala
$ scala Test
$ cat test.txt
Novice Tutorial

Read user input from the screen #

Sometimes we need to receive instructions entered by the user on the screen to process the program. Examples are as follows:

8.42.2. Example #

import scala.io.\_
object Test {
   def main(args: Array[String]) {
      print("Please enter the Rookie Tutorial official website : " )
      val line = StdIn.readLine()
      println("Thank you, what you entered is: " + line)
   }
}

Post-Scala2.11 version Console.readLine abandoned, using scala.io.StdIn.readLine() method instead of.

Execute the above code, and the following information will be displayed on the screen:

$ scalac Test.scala
$ scala Test
Please enter the Rookie Tutorial official website : www.runoob.com
Thank you, what you entered is: www.runoob.com

Read content from a file #

Reading from a file is very simple. We can use Scala’s Source class andaccompanying objects to read the file. The following example demonstrates reading from a “test.txt” (previously created) file:

8.42.3. Example #

import scala.io.Source
object Test {
   def main(args: Array[String]) {
      println("The file content is:" )
      Source.fromFile("test.txt" ).foreach{
         print
      }
   }
}

Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
The file content is:
Novice Tutorial

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.