Ho una struttura di cartelle come di seguito:Come leggere i file dalla cartella delle risorse in Scala?
- main
-- java
-- resources
-- scalaresources
--- commandFiles
e che le cartelle ho i miei file che devo leggere. ecco il codice:
def readData(runtype: String, snmphost: String, comstring: String, specificType: String): Unit = {
val realOrInvFile = "/commandFiles/snmpcmds." +runtype.trim // these files are under commandFiles folder, which I have to read.
try {
if (specificType.equalsIgnoreCase("Cisco")) {
val specificDeviceFile: String = "/commandFiles/snmpcmds."+runtype.trim+ ".cisco"
val realOrInvCmdsList = scala.io.Source.fromFile(realOrInvFile).getLines().toList.filterNot(line => line.startsWith("#")).map{
//some code
}
val specificCmdsList = scala.io.Source.fromFile(specificDeviceFile).getLines().toList.filterNot(line => line.startsWith("#")).map{
//some code
}
}
} catch {
case e: Exception => e.printStackTrace
}
}
}
Che tipo di problemi potrebbero verificarsi se si utilizza getResource e trasformandolo in un file? Puoi fornire un collegamento? – akauppi
In alcune circostanze un puntatore nullo: http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file –
Questo codice è probabilmente lasciando il gestore aperto per getResourceAsStream. – Sisso