使用Java部署文本文件,应将其放置在哪里? [英] Deploying text files with Java, where should they be located?

查看:138
本文介绍了使用Java部署文本文件,应将其放置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,该应用程序从名为Countrys.txt的文本文件中读取一些数据.在Netbeans中,该文件位于项目级别的文件夹下.现在,我已经在Netbeans中对其进行了测试,现在可以将其作为jar文件部署和测试到其他机器上了. dist文件夹不包含文本文件,我已将其手动复制到dist(作为jar文件的文件夹)和dist \ lib文件夹,但出现找不到文件"错误. 我的文件没有路径信息,只是...

I have a Java application that reads some data from a text file called Countries.txt. In Netbeans the file is under the Project-level folder. Now that I have tested it in Netbeans I am ready to deploy and test it as a jar file on different machines. The dist folder does not include the text file and I have manually copied it to the dist (folder as the jar file) and dist\lib folders but I get a 'file not found' error. My file has no path information, just ...

br = new BufferedReader(new FileReader("Countries.txt"));

带有jar文件的支持文件应该放在哪里?

With a jar file where should supporting files go?

推荐答案

"new File(..)"和类似的结构(例如"new FileReader(..)")总是构造相对于当前工作目录的路径(除非您将绝对路径作为参数传递).在IDE中,这主要是项目的根文件夹(至少在eclipse中,根据您的描述,我也假设在Netbeans中).

The "new File(..)" and similar constructs like "new FileReader(..)" always construct the path relative to the current working directory (unless you pass an absolute path as parameter). Within an IDE this is mostly the project's root folder (at least in eclipse, from your description I assume also in Netbeans).

但是当您在IDE中运行它时,当前的工作目录就是您启动java.exe的目录.

But when you run it ouside the IDE the current working directory is the directory where you started java.exe.

为避免这些不一致,我建议将有问题的文件放入声明为类路径(-cp命令行参数或在jar的manifest.mf中定义)的文件夹中.然后,您可以将文件作为资源加载:

To avoid these inconsistencies I recommend to place the files in question into a folder which is declared to be in the classpath (-cp command line argument or defined in the manifest.mf within the jar). Then you can load a file as resource:

 this.getClass().getResourceAsStream( "myFile.txt");

对于只读资源,建议采用上述方法.这样的资源甚至可以放在jar文件内的根文件夹中.

Above approach is recommended for read-only resources. Such resources can even be placed in the root folder inside your jar file.

但是,如果您必须编辑文件(并将其保存回某处),事情将会变得更加复杂.

But if you have to edit the file (and save it back somewhere) the things get more complicated.

一个独立的Java应用程序通常具有以下文件夹布局:

A standalone java application usually has a folder layout like:

myProject
    bin
        start.bat
    lib
        myApp.jar
    cfg
        myApp.properties

通过双击start.bat,您将找到当前目录为myProject/bin.知道这一点很容易进行导航,例如到cfg目录:

By double clicking on the start.bat you will find the current directory to be myProject/bin. Knowing this it's easy to navigate e.g. to the cfg directory:

new File(../cfg/myApp.properties)

这对您的运行时安装有效.但是在IDE中,您可能会有另一种布局:

This is valid for your runtime installation. But within the IDE you probably have another layout:

myProject
    prod
        bin
            start.bat
        cfg
            myApp.properties
        src
            ...
    test
        ...
    lib
        myApp.jar

如果要在IDE中运行时保持相同的硬编码相对路径,则必须在IDE的lauch配置中配置工作目录.遵循上面的示例(eclipse的示例):

If you want to keep the same hard coded relative path when running in the IDE you have to configure the working directory in the lauch configuration in the IDE. Following the example above (example from eclipse):

Working directory = "${workspace_loc:MyProject/prod/bin}". 

在eclipse中,这可以在第二个选项卡参数"的运行/运行配置"中完成.我相信Netbeans也有这种可能性.

In eclipse this can be done in Run/RunConfigurations on the second Tab "Arguments". I am sure Netbeans has such a possibility too.

这篇关于使用Java部署文本文件,应将其放置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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