Java - 在 Web 服务器目录中创建文件夹和文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10533268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - creating a folder and file in web server directory
提问by user1386809
i would like to create a file in a folder (TestFolder) of web server. I will i get relative path of web server directory and how will i create my TestFolder in that directory.
我想在 Web 服务器的文件夹(TestFolder)中创建一个文件。我将获得 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)
目前我将在本地服务器中创建一个文件夹,但稍后我需要在测试环境中创建一个文件夹(服务器在测试环境中可能不同)
回答by Rakesh
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. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
file.mkdirs()
创建由此抽象路径名命名的目录,包括任何必要但不存在的父目录。请注意,如果此操作失败,则它可能已成功创建了一些必要的父目录。
Returns:true
if and only if the directory was created, along with all necessary parent directories; false
otherwise
返回:true
当且仅当目录已创建,以及所有必要的父目录;false
否则
Hope this helps !
希望这可以帮助 !