简体   繁体   English

使用java中的File.listFiles()在Linux环境中的项目目录中列出文件

[英]Listing files in project directory in Linux environment using File.listFiles() in java

String path = ".";
String files="";
File folder = new File(path);
File[] listOfFiles = folder.listFiles(); 

for (int i = 0; i < listOfFiles.length; i++) 
{
  if (listOfFiles[i].isFile()) 
  {
     files = listOfFiles[i].getName();
     System.out.println("files::::::::::::"+files);
  }
}

Say the above java file I saved under the path D:/spike/FileList.java . 说我在路径D:/spike/FileList.java下保存的上述java文件。

In the above code if I run in windows platform , it will list the files under the ' spike ' directory. 在上面的代码中,如果我在windows platform运行,它将列出' spike '目录下的文件。

But when I keep this in Linux environment say the path is usr/local/apache/webapps/webtest/src/FileList.java 但是当我在Linux environment保留它时,说路径是usr/local/apache/webapps/webtest/src/FileList.java

The result I get is files under root directory. 我得到的结果是根目录下的文件。

What I require is under the project root folder, ie in above case under the webtest directory. 我需要的是在项目根文件夹下,即在webtest目录下的上述情况下。

How can I do the same. 我怎么能这样做。 My requirement is I need to first list the files and then from the list of files I need to read sample.properties file. 我的要求是我需要首先列出文件,然后从我需要读取的文件列表中读取sample.properties文件。

I know we can hard code the path to get the same. 我知道我们可以通过硬编码来获得相同的路径。 But without hard coding how can I get the list of files under the project root folder of webapps ie, under webtest folder in my case. 但是如果没有硬编码,我怎样才能获得webapps的项目根文件夹下的文件列表,即在我的情况下在webtest文件夹下。

I also tried by reading the environmental variables. 我也试过阅读环境变量。 But the problem here is my apache folder owner is spike and not root. 但这里的问题是我的apache文件夹所有者是spike而不是root。 So when I execute System.getenv() what I get is only whatever variables the user spike has set. 因此,当我执行System.getenv() ,我得到的只是用户峰值设置的任何变量。

But when I execute System.getenv() from any other folder whose owner is root, then I get the complete environmental variables. 但是当我从其所有者是root的任何其他文件夹执行System.getenv() ,我得到完整的环境变量。

So is there any way I can get the project root folder by using the above java code snippet without hard coding the path? 那么有什么办法可以通过使用上面的java代码片段获得项目根文件夹而无需硬编码路径?

By the way this is a web application deployed in tomcat. 顺便说一下,这是在tomcat中部署的Web应用程序。 First the app will read the details from the server.properties file. 首先,应用程序将从server.properties文件中读取详细信息。 But Im not supposed to hard code the path as the path changes from system to system. 但是当路径从一个系统变为另一个系统时,我不应该对路径进行硬编码。 So my intention is that the code read the properties file from the project starting folder. 所以我的意图是代码从项目起始文件夹中读取属性文件。

This is not true, your code on Linux prints the files in the current folder. 事实并非如此,Linux上的代码会打印当前文件夹中的文件。 I just tried it. 我刚尝试过。

As to tomcat, see here What determines the current working directory of Tomcat Java process? 至于tomcat,请参阅此处什么决定了Tomcat Java进程的当前工作目录? and/or look for similar information on the Tomcat site. 和/或在Tomcat站点上查找类似信息。

You can try with the current working directory: 您可以尝试使用当前的工作目录:

String path = System.getProperty("user.dir");

But this depends where exactly is the CWD, eg how was this application started? 但这取决于CWD的确切位置,例如该应用程序是如何启动的?


In the context of a web application, you can do (as long as you have the ServletContext object): 在Web应用程序的上下文中,您可以执行(只要您拥有ServletContext对象):

ServletContext application = ...
String path = application.getRealPath("/");

This will give you the root of the web application, you can navigate to the required directory from there. 这将为您提供Web应用程序的根目录,您可以从那里导航到所需的目录。

If your file sample.properties is located in the root folder, and actually is a properties-file I would try this loading the file and printing the content: 如果您的文件sample.properties位于根文件夹中,并且实际上是属性文件,我会尝试加载文件并打印内容:

    String filename = "sample.properties";
    Properties properties = new Properties();
    properties.load(getClass().getClassLoader().getResource(filename).openStream());
    properties.list(System.out);

getClass.getClassLoader() will get the location of the rootfolder. getClass.getClassLoader()将获取rootfolder的位置。 If you skip getClassLoader you will get the folder of the package in your class. 如果你跳过getClassLoader你将获得你班级中包的文件夹。

(If you are running this in a standalone java app the properties file needs to be located in the classes folder.) (如果您在独立的Java应用程序中运行它,则属性文件需要位于classes文件夹中。)

Thanks to all your valuable suggestions & I was able to solve it. 感谢您的所有宝贵建议,我能够解决它。 The problem was, in linux Im starting tomcat server using services like 'service tomcat start' commands. 问题是,在linux中我使用'service tomcat start'命令等服务启动tomcat服务器。 These services are in root folder. 这些服务位于根文件夹中。 So when start tomcat using them and check my current working directory I get the root folder as PWD and not my project folder. 因此,当启动tomcat使用它们并检查我当前的工作目录时,我将根文件夹作为PWD而不是我的项目文件夹。 So what i did is I started tomcat using startup.sh in $TOMCAT_DIR/bin. 所以我做的是我在$ TOMCAT_DIR / bin中使用startup.sh启动了tomcat。 Now my current PWD returns $TOMCAT_DIR/bin. 现在我当前的PWD返回$ TOMCAT_DIR / bin。 Through this I changed the path according to my requirement and was able to access the properties files. 通过这个我根据我的要求改变了路径,并能够访问属性文件。 But now my problem is I need to restart tomcat server via my application which is inside webapps. 但现在我的问题是我需要通过我的应用程序重启tomcat服务器,这是在webapps内部。 So I put this script given below: 所以我把这个脚本给出如下:

restart.sh restart.sh

cd ../../bin/
./shutdown.sh
wait
sleep 2s  
./startup.sh
wait
sleep 2s
read -p "Close the terminal"

As per above script my tomcat should succesfully restart. 按照上面的脚本,我的tomcat应该成功重启。 But its not doing so. 但它没有这样做。 I also tried running manually the same above script. 我也尝试手动运行相同的上述脚本。 In that case it will work provided I dont close the terminal. 在那种情况下,如果我不关闭终端,它将工作。 When I close the terminal again I lose contact with the server. 当我再次关闭终端时,我失去了与服务器的联系。 Any idea why it might be causing. 不知道为什么会造成这种情况。 Btw for executing the above script from my java code the below is steps I followed: 顺便说一句,从我的java代码执行上面的脚本,下面是我遵循的步骤:

String path = "../webapps/webtest/";
try {
    Runtime.getRuntime().exec(path+"restart.sh");
} catch (IOException e) {
    System.out.println("Exception while running script: "+e);
}

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

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