从一个文件中获取InputStream,该文件可能(也可能不)在类路径中 [英] Get InputStream from a file, that may (or not) be in the classpath

查看:77
本文介绍了从一个文件中获取InputStream,该文件可能(也可能不)在类路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只想知道哪种方法是读取可能位于类路径中的文件的最佳方法.

Just wanted to know which is the best way to read a file that may be in the classpath.

我唯一拥有的是带有文件路径的属性.例如:

The only thing i have is a property with the path of the file. For exemple:

  • filepath = classpath:com/mycompany/myfile.txt
  • filepath = file:/myfolder/myfile.txt

从该属性加载InputStream的最佳方法是什么?

Which is the best way to load an InputStream from that property?

推荐答案

您可以使用

You can use the URL method openStream, which returns an InputStream you can use to read your file. The URL will work for files inside and outside the JAR. Just take care to use a valid URL.

这篇关于从一个文件中获取InputStream,该文件可能(也可能不)在类路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆