How to change System.getProperty(“user.dir”) to project workspace

后端 未结 5 1389
深忆病人
深忆病人 2020-12-06 18:54

I try to read a .txt file line by line in my code, which I placed it just right under the /src/ directory, when I run it with test case or with static void main

5条回答
  •  情书的邮戳
    2020-12-06 19:37

    To read files from the root of the classpath use (eclipse automatically copies any non java file from src to classes):

    this.getClass().getClassLoader().getResourceAsStream("filename.txt");
    

    So you don't have to mess with the current folder at all.

提交回复
热议问题