![](/img/trans.png)
[英]Reading file from /src/main/resources gives exception FileNotFound
[英]Reading file from src/main/resources as File?
我的項目結構看起來像
src/main/resources/bluecoat_100_lines.log
我的代碼看起來像
object Main {
def main(args: Array[String]) {
val system = ActorSystem("logprocessor")
val file: File = new File("src/main/resources/bluecoat_100_lines.log")
val logFile = LogFile(file, system)
logFile.process()
}
}
和
object LogFile {
def apply(file: File, system: ActorSystem) = new LogFile(file, system)
}
class LogFile(file: File, system: ActorSystem) {
val logger = Logger(LoggerFactory.getLogger(getClass))
def process() = {
Predef.assert(file.exists(), "log file must exists")
logger.debug(s"processing $file")
}
}
目前我得到
Exception in thread "main" java.lang.AssertionError: assertion failed: log file must exists
at scala.Predef$.assert(Predef.scala:165)
at com.learner.processor.LogFile.process(LogFile.scala:24)
at com.learner.logprocessor.Main$.main(Main.scala:15)
at com.learner.logprocessor.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
如何打開資源文件作為File
而不是InputStream
作為getClass.getResourceAsStream
?
File f = new File("your file directory here.");
另外,您的getClass.getResourceAsStream
在哪里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.