Java reads files in the resource directory
Put the file in the resouce directory
code
import java.io.*;
public class FileUtilsTest {
public static void main(String[] args) throws IOException {
String filePath = "123.txt";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(filePath);
if (inputStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
}
} else {
System.out.println("文件未找到:" + filePath);
}
}
}