Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the project path in Scala?

I'm trying to read some files from my Scala project, and if I use: java.io.File(".").getCanonicalPath() I find that my current directory is far away from them (exactly where I have installed Scala Eclipse). So how can I change the current directory to the root of my project, or get the path to my project? I really don't want to have an absolute path to my input files.

  val PATH = raw"E:\lang\scala\progfun\src\examples\"
  def printFileContents(filename: String) {
    try {
      println("\n" + PATH + filename)
      io.Source.fromFile(PATH + filename).getLines.foreach(println)
    } catch {
      case _:Throwable => println("filename " + filename + " not found")
    }
  }

  val filenames = List("random.txt", "a.txt", "b.txt", "c.txt")
  filenames foreach printFileContents
like image 205
carlos_lm Avatar asked Sep 25 '14 09:09

carlos_lm


People also ask

How do I set path in project?

In the Project > Files view, you can use the context menu to add or remove folders from the project path. Right-click a folder and select Project Path > Add to Project Path, or Add to the Project Path (Including Subfolders), or one of the options to remove the folder from the path.

How do I get the current working directory in Pyspark?

csv file in spark, copy this file to all workers under same path(say /tmp/data. csv). Now you can use sc. textFile("file:///tmp/data.csv") to create RDD.


1 Answers

  1. Add your files to src/main/resources/<packageName> where <packageName> is your class package.
  2. Change the line val PATH = getClass.getResource("").getPath
like image 59
Andrzej Jozwik Avatar answered Sep 25 '22 15:09

Andrzej Jozwik