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"));
returns an error.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Assuming the text file is in the same directory as the
.class
, rather than the.java
file you can doWhat you have will look for the file in the current working directory. When you are building your program this is typically the root directory of your program. When you run it as a standalone program it is usually the directory the program was started from.
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