以编程方式在Java资源/源文件夹中创建文件? [英] Create file in resources/source folder in java programmatically?

查看:768
本文介绍了以编程方式在Java资源/源文件夹中创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个资源文件夹。

src-这是我的.java文件

src - here are my .java files

资源-此处

是否可以通过编程方式在该资源文件夹中添加另一个.properties文件?

Is there a way to programmatically add another .properties file in that resources folder?

p>

我尝试过这样的事情:

I tried something like this:

public static void savePropertiesToFile(Properties properties, File propertiesFile) throws IOException {
        FileOutputStream out = new FileOutputStream(propertiesFile);
        properties.store(out, null);
        out.close();
    }

,在此之前创建:

new File("/folderInResources/newProperties.properties");

但是它会在文件系统上查找该路径。我该如何强迫它在资源文件夹中查找?

But it looks for that path on the file system. How can I force it to look in the resources folder?

编辑:让我说说它的含义。我有一个GUI应用程序,并且支持2种语言(resources文件夹中的2个.properties文件)。现在,我添加了一个选项,用户可以轻松地翻译应用程序,当他完成操作时,我会将新的.properties保存在磁盘中的某个隐藏文件夹中,然后从那里读取它。但是我希望可以将这些新的.properties文件(新语言)保存在当前语言(资源文件夹)旁边。我有一个静态的Messages类,它知道如何从磁盘以及资源文件夹中的默认资源加载资源。但是,如果用户在其他计算机上使用此.jar文件,则他将不会具有这些新语言,因为它们位于该计算机的磁盘上,而不位于.jar文件中。

EDIT: Let me say what is it about. I have a GUI application and I support 2 languages (2 .properties files in resources folder). Now I added a option that user can easily translate application and when he finishes I save that new .properties on a disk in some hidden folder and read it from there. But I was hoping I could save that new .properties files (new language) next to the current languages (resources folder). I have a static Messages class which knows how to load resources both from the disk and both the default ones in resources folder. But if user takes this .jar file on some other machine, he would't have that new languages since they are on disk on that computer, not inside .jar file.

推荐答案

正如其他人所提到的,资源是通过ClassLoader获得的。但是,当前两个响应未能强调的是以下几点:

As other people have mentioned, resources are obtained through a ClassLoader. What the two current responses have failed to stress, however, is these points:


  • ClassLoaders旨在抽象获取课程和其他资源的过程。资源不必是文件系统中的文件。它可以是远程URL,也可以是您或其他人通过扩展 java.lang.ClassLoader 可以实现的所有功能。

  • ClassLoader存在于子/父委托链中。 ClassLoader的正常行为是,首先尝试从父级获取资源,然后才搜索其自身的资源,但是某些类加载器执行相反的顺序(例如,在servlet容器中)。无论如何,您都需要确定要在哪个类加载器中放置要放入的内容,甚至在其上方或下方的另一个类加载器都可能窃取客户端代码的资源请求。

  • 正如Lionel Port指出的那样,即使单个ClassLoader可能也会在多个位置加载东西。

  • ClassLoader通常用于加载类。如果您的程序可以将文件写入到加载了类的位置,则这很容易成为安全隐患,因为用户可能会将代码注入到正在运行的应用程序中。

  • ClassLoaders are meant to abstract the process of obtaining classes and other resources. A resource does not have to be a file in a filesystem; it can be a remote URL, or anything at all that you or somebody else might implement by extending java.lang.ClassLoader.
  • ClassLoaders exist in a child/parent delegation chain. The normal behavior for a ClassLoader is to first attempt to obtain the resource from the parent, and only then search its own resources—but some classloaders do the opposite order (e.g., in servlet containers). In any case, you'd need to identify which classloader's place for getting stuff you'd want to put stuff into, and even then another classloader above or below it might "steal" your client code's resource requests.
  • As Lionel Port points out, even a single ClassLoader may have multiple locations from which it loads stuff.
  • ClassLoaders are used to, well, load classes. If your program can write files to a location where classes are loaded from, this can easily become a security risk, because it might be possible for a user to inject code into your running application.

简短版本:请勿这样做。为我可以从中获取资源的类资源的存储库的概念编写一个更抽象的接口,为我可以从中获取内容的源性材料的存储库添加子接口。通过使用 ClassLoader.getContextClassLoader()。getResource()(以搜索类路径)以及如果失败的方式,使用某种其他机制来获取东西该程序可能是从某个位置添加的。

Short version: don't do it. Write a more abstract interface for the concept of "repository of resource-like stuff that I can get stuff from," and subinterface for "repository of resource-like stuff that I can get stuff from, but also add stuff from." Implement the latter in a way that both uses ClassLoader.getContextClassLoader().getResource() (to search the classpath) and, if that fails, uses some other mechanism to get stuff that the program may have added from some location.

这篇关于以编程方式在Java资源/源文件夹中创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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