0

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 !!

programmer420
  • 47
  • 1
  • 11
  • You could use absolute path instead of providing relative path. – Rahul Agrawal Oct 31 '19 at 06:29
  • but some requerment it not possible to use absolute path. Thanks for your queck reply. – programmer420 Oct 31 '19 at 06:32
  • Does this answer your question? [How to write path to go one level up and then down into another directory](https://stackoverflow.com/questions/9999743/how-to-write-path-to-go-one-level-up-and-then-down-into-another-directory) – www.hybriscx.com Oct 31 '19 at 06:45
  • Once you package your application as a Jar/War, that `src` folder will not be available at all, so re-think what you're doing. – Andreas Oct 31 '19 at 07:17

3 Answers3

2

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.

VadymVL
  • 5,366
  • 3
  • 26
  • 41
0

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

Kahn Sparkle
  • 133
  • 1
  • 10
0

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

peridin
  • 28
  • 3