简体   繁体   English

如何在工作区外部的文件夹中的java类中读取属性文件?

[英]How can I read properties file in java class located in a folder outside workspace?

I have a properties file located in C:/codebase/myProject-Authentication/sample.properties , and my workspace is at C:/codebase/myProject/com.company.team.website/src/.../AccessCodeServlet.java . 我在C:/codebase/myProject-Authentication/sample.properties有一个属性文件,我的工作空间在C:/codebase/myProject/com.company.team.website/src/.../AccessCodeServlet.java

So, I need to read sample.properties from AccessCodeServlet.java . 因此,我需要从AccessCodeServlet.java读取sample.properties AccessCodeServlet is a servlet. AccessCodeServlet是一个servlet。

If the properties file is in the same folder as AccessCodeServlet.java , I can just do this: 如果属性文件与AccessCodeServlet.java位于同一文件夹中,则可以执行以下操作:

ResourceBundle bundle = ResourceBundle.getBundle("sample");

But how do I do it when properties file is outside workspace? 但是,当属性文件位于工作空间之外时,该怎么办?

Move the file to a relative path to the workspace before usage? 使用之前将文件移动到工作空间的相对路径吗?

public void MovePropertiesFile()
{   

    InputStream otherStream = null;
    OutputStream workspaceStream = null;

    try{

        File afile =new File("C:\\somwhere\\.....\\properties.properties");
        File bfile =new File("C:\\workspace\\.....\\properties.properties");

        otherStream = new FileInputStream(afile);
        workspaceStream = new FileOutputStream(bfile);

        byte[] buffer = new byte[1024];

        int length;
        while ((length = otherStream .read(buffer)) > 0){

            workspaceStream .write(buffer, 0, length);

        }

        otherStream .close();
        workspaceStream .close();


    }catch(IOException e){
        e.printStackTrace();
    }
}

Well, you can add an external folder containing your sample.properties to the classpath in eclipse: 好了,您可以将包含sample.properties的外部文件夹添加到eclipse中的类路径:

  • Open Run Configuration 开放式运行配置
  • Select <Your app entry> 选择<您的应用程序条目>
  • Go to "Classpath" tab 转到“类路径”选项卡
  • Select "User entries" 选择“用户条目”
  • Click "Advanced" button 点击“高级”按钮
  • Check "Add external folder" 选中“添加外部文件夹”
  • Click "OK" button 点击“确定”按钮
  • Select your external folder with .properties file(s) 选择带有.properties文件的外部文件夹

Viola - now resource bundles are found! 中提琴-现在找到资源包!

You need to specify absolute path but you shouldn't hard-code it as you correctly surmised. 您需要指定绝对路径,但您不应在正确推测的情况下对其进行硬编码。

You should get the base path of the file(s) from a System property which you can access using System.getProperty("basePath") in your code and which should be prepended to your file name to create an absolute path. 您应该从系统属性中获取文件的基本路径,可以在代码中使用System.getProperty(“ basePath”)访问该系统属性,并且应该在文件名之前添加以创建绝对路径。

While running your application you can specify the path in java command line as follows: 运行应用程序时,可以在java命令行中指定路径,如下所示:

java -DbasePath="/a/b/c" ...

... signifies the current arguments to your Java command to run your program. ...表示运行命令的Java命令的当前参数。

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

相关问题 如何从Class文件夹外部读取java中的属性文件? - How to read a properties file in java from outside the Class folder? 如何读取位于项目文件夹java中的文件 - How to read a file that is located in the project folder java 如何读取位于eclipse中java项目文件夹中的bin文件 - How to read bin file located in java project's folder in eclipse 如何访问位于项目文件夹外部和项目类路径外部的.properties文件? - How can I access to a .properties file situated outside the project folder and outside the project classpath? 如何在Java上按字典顺序阅读类属性(成员)? - how can i read class properties (members) in lexicographic order on java? 如何在Java(ubuntu)中通过FileInputStream读取属性文件 - How can I read properties file through FileInputStream in java (ubuntu) 如何读取Java资源文件夹中的文件? - How can I read my file which is in a resources folder in Java? 如何使用Java Netbeans读取项目文件夹外部的txt文件 - How to read in txt file outside of project folder with Java Netbeans 如何使用Java在tomcat webapps文件夹之外读写图像 - How can I read and write Image outside tomcat webapps folder using java 如何在我的程序中加载文本文件而不将其放在源文件夹之外? - How do I load a text file in my program without it being located outside of the sources folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM