Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 158 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Spring - How to use classpath to specify a file location?

#1
How can I use the classpath to specify the location of a file that is within my Spring project?

This is what I have currently:

FileReader fr = new FileReader("C:\\Users\\Corey\\Desktop\\storedProcedures.sql");

This is hardcoded to my Desktop. What I would like is to be able to use the path to the file that is in my project.

FileReader fr = new FileReader("/src/main/resources/storedProcedures.sql");

Any suggestions?
Reply

#2
Are we talking about standard [`java.io.FileReader`](

[To see links please register here]

)? Won't work, but it's not hard without it.

`/src/main/resources` maven directory contents are placed in the root of your CLASSPATH, so you can simply retrieve it using:

InputStream is = getClass().getResourceAsStream("/storedProcedures.sql");

If the result is not `null` (resource not found), feel free to wrap it in a reader:

Reader reader = new InputStreamReader(is);
Reply

#3
looks like you have maven project and so resources are in classpath by

go for

getClass().getResource("classpath:storedProcedures.sql")
Reply

#4
Spring has org.springframework.core.io.Resource which is designed for such situations. From context.xml you can pass classpath to the bean

<bean class="test.Test1">
<property name="path" value="classpath:/test/test1.xml" />
</bean>

and you get it in your bean as Resource:

public void setPath(Resource path) throws IOException {
File file = path.getFile();
System.out.println(file);
}
output

D:\workspace1\spring\target\test-classes\test\test1.xml

Now you can use it in new FileReader(file)


Reply

#5
From an answer of @NimChimpsky in [similar question][1]:

Resource resource = new ClassPathResource("storedProcedures.sql");
InputStream resourceInputStream = resource.getInputStream();

Using [ClassPathResource][2] and interface [Resource][3]. And make sure you are adding the resources directory correctly (adding `/src/main/resources/` into the classpath).

Note that Resource have a method to get a `java.io.File` so you can also use:

Resource resource = new ClassPathResource("storedProcedures.sql");
FileReader fr = new FileReader(resource.getFile());


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through