答案 0 :(得分:3)
对于像这样的项目结构:
project
|
| -> src
| |
| | -> directory
| | |
| | | -> MyClass.java
| | |
| | | -> file.txt
使用以下代码读取MyClass.java中的文件
import java.io.*;
class MyClass {
public static void main(String... args) {
try {
BufferedReader br = new BufferedReader(new FileReader("src/directory/file.txt"));
// read your file
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}