spring 从类路径加载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15297557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:47:57  来源:igfitidea点击:

loading a file from classpath

springfileclasspath

提问by Steer360

I have a line of code that is : File file = new File(getFile())in a java class HandleData.java

我有一行代码:File file = new File(getFile())在 java 类中HandleData.java

Method - getFile()takes the value of the property fileName. And fileNameis injected through application_context.xmlwith a bean section of the class - HandleData as below:

方法 -getFile()获取属性的值fileName。并fileName通过application_context.xml类的 bean 部分注入 - HandleData 如下:

 <bean id="dataHandler" class="com.profile.transaction.HandleData">
 <property name="fileName" value="DataFile.xml"></property>
 </bean>

I build the project successfully and checked that - DataFile.xmlis present in WEB-INF/classes. And the HandleData.class is present in WEB-INF/classes/com/profile/transacon

我成功构建了项目并检查了 -DataFile.xml是否存在于WEB-INF/classes. 并且 HandleData.class 存在于WEB-INF/classes/com/profile/transacon

But when I run it it throws me filenotfound exception. If I inject the absolute path (C:\MyProjectWorkspace\DataProject\target\ProfileService\WEB-INF\classes\DataFile.xmlit finds the file successfully.).

但是当我运行它时,它会抛出 filenotfound 异常。如果我注入绝对路径(C:\MyProjectWorkspace\DataProject\target\ProfileService\WEB-INF\classes\DataFile.xml它成功找到文件。)。

Could someone help in figuring out the proper path to be injected so that the file is taken from the classpath ?

有人可以帮助找出要注入的正确路径,以便从类路径中获取文件吗?

回答by helmy

While injecting a Fileis generally the preferred approach, you can also leverage Spring's ResourceLoader for dynamic loading of resources.

虽然注入 aFile通常是首选方法,但您也可以利用 Spring 的 ResourceLoader 来动态加载资源。

Generally this is as simple as injecting the ResourceLoaderinto your Spring bean:

通常,这就像将 注入ResourceLoader到您的 Spring bean 中一样简单:

@Autowired
private ResourceLoader resourceLoader;

Then to load from the classpath:

然后从类路径加载:

resourceLoader.getResource("classpath:myfile.txt");

回答by Lucas

You should have:

你应该有:

<property name="fileName" value="classpath:DataFile.xml" />

And it should be injected as a org.springframework.core.io.Resourcesimilar to this answer

它应该被注入的org.springframework.core.io.Resource类似这样的回答

回答by Sudhakar

Since OP is injecting Only the fileName through spring, still want to create the File Object through code , You should Use ClassLoadeer to read the file

由于OP是通过spring只注入文件名,还是想通过代码创建文件对象,你应该使用ClassLoadeer来读取文件

Try this

尝试这个

InputStream is =  HandleData.class.getClassLoader().getResourceAsStream(getFile()));

Edit

编辑

Heres the remainder of code , to read the file

下面是代码的其余部分,用于读取文件

BufferedInputStream bf = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bf);

while (dis.available() != 0) {
    System.out.println(dis.readLine());
}

Edit 2

编辑 2

Since you want it as File Object, to get hold of the FileInputStream

既然你想把它作为文件对象,就可以得到 FileInputStream

try this

尝试这个

 FileInputStream fisTargetFile = new FileInputStream(new File(HandleData.class.getClassLoader().getResource(getFile()).getFile()));