Joy
Joy

Reputation: 71

how to read file from resource folder in eclipse

I'm trying to read an xml file from resource folder which is inside WebContent in the eclipse project. The problem is that i'm unable to read the file from resources folder but its working fine by giving a path outside(ie,E:/new/test.xml).

File file = new File(request.getSession().getServletContext().getContextPath()+"/resources/new/test.xml");

Can anyone please help me to solve the issue..Thanks in advance.

Upvotes: 2

Views: 4876

Answers (3)

Musaddique S
Musaddique S

Reputation: 1567

this.getClass().getResource("new/test.xml");

or you can directly use file object

File file=new File("new/test.xml");

Upvotes: 0

Essex Boy
Essex Boy

Reputation: 7950

You can read content from the classpath as follows :

getClass().getResourceAsStream("/new/test.xml");

In eclipse Webcontent is not in the classpath, by default the only thing in the classpath is /src/test to see what's in your classpath go to the project-settings -> Java Build Path, then add folders on the source tab, all the folders will then be in the classpath.

Upvotes: 1

devops
devops

Reputation: 9187

If you are working with spring, so use spring capabilities:

@Value("classpath:new/test.xml")
private Resource resource;

public void doSomeThing(){

    File file = resource.getFile();

    [...]
}

enter image description here

Upvotes: 0

Related Questions