Pergunta

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.

Foi útil?

Solução 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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top