문제

I have some data files in my WebApplication which I want to read.

How can I access them from an EJB?

I tried something like this but it didn't work:

@Singleton
public class Server {

  public void loadData() {
    InputStream is =
      this.getClass().getClassLoader().
      getResourceAsStream("WebContent/WEB-INF/Data/Data.xml");
    //read from is...
  }

}

Or is there any better way to handle read-only data files? I don't want to use a database, because I never have to write into these files, and I can parse big XML files very easily.

도움이 되었습니까?

해결책 2

After some experimenting, I finally got it working. The Problem was the wrong Path. Right is:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("../Data/Data.xml");

(I don't know why)...

InputStream is = this.getClass().getClassLoader().getResourceAsStream("classpath:Data/Data.xml");

is not working for me but thanks for your help.

다른 팁

try:

public void loadData() {
  InputStream is = this.getClass().getClassLoader().getResourceAsStream("classpath:Data/Data.xml");
  //read from is...
}

More details here: URL to load resources from the classpath in Java

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top