programmer420
programmer420

Reputation: 47

How to read a file content which is out of /src in java or spring?

My project Structure like,

MyTest
    |--> src
          |--> .... all java codes...
    |--> www
          |--> myfile.txt

i tryed to solve it by Resource resource1 = new ClassPathResource("classpath:myfile.txt"); but it couldn`t find file. also i search it in Stackoverflow but no actual answer related to this.

how to i read contents of myfile.txt in my Test.java ?

any one have solution please share with me . Thanks !!

Upvotes: 0

Views: 1332

Answers (3)

VadymVL
VadymVL

Reputation: 5566

If you are using a Maven as your build tool, you need to include your www directory to the classpath in your pom.xml:

 <build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <directory>src/main/www</directory>
        </resource>
    </resources>
 </build>

You can read more about it here.

Upvotes: 2

peridin
peridin

Reputation: 28

The file is in the class path folder then it will work. classpath: filename is not needed. Just give the file name as parameter

Upvotes: 0

Kahn Sparkle
Kahn Sparkle

Reputation: 133

You are using what IDE? If you are using IntelliJ , you should set the containing folder of your file as resources

Upvotes: 0

Related Questions