简体   繁体   English

从Eclipse运行时,Java应用程序应在哪里创建文件?

[英]Where should a java application create files when it is run from Eclipse?

When my Java application is run with "Run application" from Eclipse it needs to create files that persist across multiple runs of the application. 当我的Java应用程序与Eclipse中的“运行应用程序”一起运行时,它需要创建在应用程序的多次运行中持久存在的文件。 Is it an acceptable practice to place these in the "target" directory? 将它们放在“目标”目录中是否可以接受? For example, workspace//target/database.db. 例如,workspace // target / database.db。

Are there events that cause eclipse to delete all files in the target directory? 是否有导致eclipse删除目标目录中所有文件的事件?

If the target directory is not a good place to store these files where is a good place? 如果目标目录不是存放这些文件的好地方,哪里是好地方? It would be awkward to use System.getProperty("java.io.tmpdir") since the tmpdir is on a different drive and because I would need to mirror the structure of the workspace and project in the tmpdir. 使用System.getProperty(“ java.io.tmpdir”)会很尴尬,因为tmpdir位于不同的驱动器上,并且因为我需要在tmpdir中镜像工作区和项目的结构。

Thank you 谢谢

The target directory gets deleted when you clean your project ( mvn clean ). 清理项目时, target目录将被删除( mvn clean )。 That makes this not the best place to store your data. 这使得它不是存储数据的最佳位置。 You should better save it to System.getProperty("user.home") , or pass a directory as a program argument as SpaceTrucker mentioned in the comment, that`s also a good solution. 您最好将其保存到System.getProperty("user.home") ,或者将目录作为程序参数传递,如注释中提到的SpaceTrucker一样,这也是一个很好的解决方案。

My recommendation would be the OSGi User Area ( -user , intended for user-specific preferences etc.), or the OSGi Instance Area ( -data , which is the default home of The Workspace). 我的建议是OSGi用户区域( -user ,用于用户特定的首选项等),或OSGi实例区域( -data ,这是Workspace的默认位置)。 Take a look at this , and scroll down to "Locations", which explains a bit about these areas. 看看这个 ,向下滚动到“位置”,这也解释了一些关于这些方面。

As for hazardous events, only -clean (the Eclipse arg, not the Maven target) and your deployment process come to mind. 对于危险事件,只有-clean (Eclipse arg,而不是Maven目标)和部署过程可以想到。 -clean should not touch persisted files but works on the internal caches, only, but you might want to try that out to be sure. -clean不应触摸持久文件,而只能在内部缓存上使用,但您可能需要尝试一下以确保安全。 Your deployment process might consist of "delete contents of installation directory" plus "copy new stuff to installation directory", which would wipe most of the areas which are by default placed inside the installation directory. 您的部署过程可能包括“删除安装目录的内容”加上“将新内容复制到安装目录”,这将擦除默认情况下位于安装目录中的大多数区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM