English 中文(简体)
Java档案途径的最佳做法
原标题:Best practices to work with file paths in Java

我试图装上财产档案,以建立一个配置。 即 f。

What the best place to store this config file. Within the package where i will use initialize the using class? on the root level of the src?

此外,在查阅档案时,即使用档案系统中的档案地点:e:c:configsfoo.properties。

是否有更好的做法? 例如,使用与项目而不是档案系统所在地有关的途径? 如果是,如何在项目树内提及财产档案的位置。 例如:

src/
   /com.foo.bar
   /com.foo.car
   /com.foo.zar
   foo.properties

让我们说,想读一下财产档案,那么,如何利用本组织所在地或档案系统中的位置来查阅这一oo。

最佳回答

如果你想要与你目前的结构保持接近,那么你就能够补充一个<>资源代码的倍数。

src/
   /com.foo.bar
   /com.foo.car
   /com.foo.zar
resources
   /foo.properties

But I highly recommend following Maven directory structures, they really do give you a lot of flexibility:

src/
    /main
        /java
            /com.foo.bar
            /com.foo.car
            /com.foo.zar
        /resources
            /foo.properties

不管你有何规定,我建议把你的资源放在与你 Java一揽子计划相匹配的双管结构中,这样,你就可以轻易地把资源装上,避免你对相对立案道路的绝对 v。 与此类似:

src/
    /resources/
       /com/foo/bar/foo.properties

并且给他们一个班子。 你们可以这样做:

InputStream is = MyClass. getResourceAsStream("com/foo/bar/foo.properties");
问题回答

我总是看到的是,如何将财产档案放在北极圈/主要/资源中,然后用ResourceAsStream()。

这将是一个很好的问题:。 替代载于Ajar的资源

我认为,地点确实取决于你的项目。 我通常希望有专门的文件夹来储存配置文件。

关于你查阅档案的方式,使用绝对道路确实是一种坏ide: 想把你的申请散发为口头申请,在网络服务器上部署,或者简单地将其转移到另一个地方!

研究:

如果贵阶层拥有财产档案(例如,作为你的来源的一部分),那么,你可以单独装载(即使是在狱中)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签