Ask Your Question
4

Is it impossible to open a file with relative paths in Scala?

asked 2023-06-23 13:49:01 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-23 14:07:02 +0000

huitzilopochtli gravatar image

No, it is not impossible to open a file with relative paths in Scala. In fact, Scala provides several libraries and methods for working with files, including those with relative paths. Here's an example:

import scala.io.Source

val filePath = "src/main/resources/data.txt"  // relative path to file
val fileSource = Source.fromFile(filePath)   // open file using relative path
val fileContent = fileSource.mkString        // read file content
fileSource.close()                           // close file

println(fileContent)  // print content of file

In this example, we use the scala.io.Source library to open a file with the relative path src/main/resources/data.txt. The fromFile method is used to open the file, and mkString is used to read the content of the file as a string. Finally, we close the file using the close method. The content of the file is then printed to the console.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-23 13:49:01 +0000

Seen: 18 times

Last updated: Jun 23 '23