Tôi có một cấu trúc thư mục như dưới đây:Cách đọc tệp từ thư mục tài nguyên trong Scala?
- main
-- java
-- resources
-- scalaresources
--- commandFiles
và trong các thư mục mà tôi có tập tin của tôi rằng tôi phải đọc. Đây là mã:
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
}
}
}
Những loại vấn đề có thể xảy ra nếu sử dụng getResource và biến nó thành một tập tin? bạn có thể cung cấp một liên kết? – akauppi
Trong một số trường hợp, một Con trỏ rỗng: http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file –
Mã này có thể là để mở trình xử lý mở cho getResourceAsStream. – Sisso