Java - Getting file from same package

前端 未结 4 1564
甜味超标
甜味超标 2021-01-11 13:16

If I want to read from \"Words.txt\" which is in the same package as the class, how would I do this? Doing simply Scanner = new Scanner(new File(\"Words.txt\"));

4条回答
  •  伪装坚强ぢ
    2021-01-11 13:34

    Scanner = new Scanner(new File("/path/to/Words.txt")); 
    

    The argument in the File() constructor, Is the path relative to the system your VM is running on, it s doesn't depend on the classe's package.

    If you your words.txt is a resource packaged with your war you can see here : Load resource from anywhere in classpath

提交回复
热议问题