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
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.
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.
src/main/resources/<packageName>
where <packageName>
is your class package.val PATH = getClass.getResource("").getPath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With