简体   繁体   English

Java - 在Web服务器目录中创建文件夹和文件

[英]Java - creating a folder and file in web server directory

i would like to create a file in a folder (TestFolder) of web server. 我想在Web服务器的文件夹(TestFolder)中创建一个文件。 I will i get relative path of web server directory and how will i create my TestFolder in that directory. 我将获得Web服务器目录的相对路径,以及如何在该目录中创建我的TestFolder。

currently i will create a folder in my local server but later i need to create a folder in test environment (server could be different on test environment) 目前我将在我的本地服务器中创建一个文件夹,但后来我需要在测试环境中创建一个文件夹(服务器可能在测试环境中有所不同)

String realPath = getServletContext().getRealPath("/");
File file = new FIle(realPath+"/TestFolder", "testFIle.txt");
file.mkdirs();

The above code will create a folder TestFolder and a file testFIle.txt in the root directory of your web application. 上面的代码将在Web应用程序的根目录中创建一个文件夹TestFolder和一个文件testFIle.txt

file.mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. file.mkdirs()创建此抽象路径名所指定的目录,包括任何必需但不存在的父目录。 Note that if this operation fails it may have succeeded in creating some of the necessary parent directories. 请注意,如果此操作失败,则可能已成功创建一些必需的父目录。

Returns: true if and only if the directory was created, along with all necessary parent directories; 返回:当且仅当创建目录时才返回 true ,以及所有必需的父目录; false otherwise 否则是false

Hope this helps ! 希望这可以帮助 !

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

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