java - 从资源中获取文件名(带路径)

标签 java apache-camel

我正在使用 Camel 创建一些路线。

我在资源文件夹中有一个文件,我只想要文件路径+名称。不是内容。

当我使用时:

from(URI)
 .log("resource:classpath:llave.txt")

我得到了 llave.txt 的内容,但我需要类似的东西

C:\something\llave.txt

谢谢!!

为清楚起见进行编辑:我不需要来自文件端点的文件信息(另外,使用文件语言或交换 header 很容易获取该信息)。

我需要位于项目的资源文件夹中的文件的信息。

最佳答案

获取文件:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("llave.txt").getFile());

获取路径:

//full path ( including the file ) 
String absolutePath = file.getAbsolutePath();
// path only
String filePath = absolutePath.
    substring(0, absolutePath.lastIndexOf(File.separator));

关于java - 从资源中获取文件名(带路径),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875148/

相关文章:

java - apache camel 多播和收件人列表模式有什么区别?

java.lang.AbstractMethodError : org. apache.activemq.ActiveMQConnection.createSession 错误

Java 8 javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated, 但不是 Java 7

java - Android - 全局变量 - 应用程序错误

java - 分享 Intent 需要很长时间才能出现

java - 有没有一种方法可以在不使用 Camel 中的 .bean 关键字的情况下在我的路由中 Autowiring 一个 bean?

java - 如何在 angularjs 中使用 $resource 来使用 Spring Restful Web 服务

Java 泛型集合

java - Camel HTTP 组件似乎卡住了

apache-camel - 将 OSGi 服务公开为 Camel 端点