简体   繁体   English

NetBeans中Web应用程序的当前目录

[英]Current directory of a web app in netbeans

I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? 我在一个Web应用程序上工作,我在其中使用了某些文件的Java文件。我想在Java中使用相对路径指定这些文件,以免产生移动性问题。这样我就可以使用相对路径了吗? I have tried placing the files under source package , web folder , directly under the web-application.Please help.Thanks in advance 我已尝试将文件放置在Web应用程序下的source packageweb folder下,请提供帮助。

The simplest way to get the current directory of a java application is : 获取Java应用程序当前目录的最简单方法是:

System.out.println(new File(".").getAbsolutePath());

Like that you can consider the given path as the root of your application. 这样,您可以将给定的路径视为应用程序的根。

Cheers, Maxime. 干杯,马克西姆。

Read the file as a resource. 将该文件作为资源读取。 Put it somewhere in the src. 将其放在src中。 For instance 例如

src/resources/myresource.txt

Then you can just do 那你就可以做

InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");

Note: if you are using maven, then you are more accustomed to something like this 注意:如果您使用的是maven,那么您将更习惯于这样的事情

src/main/resources/myresource.txt

With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path 使用Maven,main / resources文件夹中的所有内容都将构建到根目录,因此您将忽略路径中的资源

InputStream is = getClass().getResourceAsStream("/myresource.txt");

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

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