Java 我正在寻找一种在SpringBoot项目中读取具有相对路径的文件的方法
我发表的战争是绝对路径。 我想读取具有相对路径的文件Java 我正在寻找一种在SpringBoot项目中读取具有相对路径的文件的方法,java,spring-boot,Java,Spring Boot,我发表的战争是绝对路径。 我想读取具有相对路径的文件 /at/tomcat/webapps/myapplication.war /test.txt 我想 /at/tomcat/webapps/text.txt 我该怎么办 Java代码 File file = new File("./test.txt"); Tomcat状态 tomcat-8080.service - Apatch tomcat8 server daemon Main PID: 5982 (code=e
/at/tomcat/webapps/myapplication.war
/test.txt
我想
/at/tomcat/webapps/text.txt
我该怎么办
Java代码
File file = new File("./test.txt");
Tomcat状态
tomcat-8080.service - Apatch tomcat8 server daemon
Main PID: 5982 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat-8080.service
mq5990 /at/jdk/bin/java -Djava.util.logging.config.file=/at/tomcat/tomcat-8080/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protoc...
Using CATALINA_HOME: /at/tomcat
1Using CATALINA_TMPDIR: /at/tomcat/tomcat-8080/temp
1Using CLASSPATH: /at/tomcat/bin/bootstrap.jar:/at/tomcat/bin/tomcat-juli.jar
1Using CATALINA_PID: /at/tomcat/tomcat-8080/tomcat-8080.pid
有两种方法 第一本手册
public Resource loadFile() {
return new ClassPathResource("data/text.txt");
}
第二种方法是使用注释
@Value("classpath:data/resource-data.txt")
Resource resourceFile;
您还可以通过其他方式查看文档的详细信息
我在下面更改了。File File=new ClassPathResource(“./test.txt”).getFile();但它仍然不起作用。java.io.FileNotFoundException:无法将类路径资源[“/test.txt”]解析为URL,因为它不存在。是否可以尝试放置完整路径?如果您使用的是linux,请确保该文件具有RWX权限。它已经读取了完整路径为
file file file=new file(“./test.txt”)的文件代码>我希望它读取同一目录中的文件。