从src / main / resources读取文件作为文件?

时间:2015-10-05 22:52:14

标签: java scala

我的项目结构看起来像

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

1 个答案:

答案 0 :(得分:-3)

File f = new File("your file directory here.");

此外,您的getClass.getResourceAsStream

在哪里