如何在 Java 应用程序的根目录中创建文件夹?
有没有办法在我的 Java 应用程序的主目录中创建一个文件夹(具有特定名称)。我的游戏中有一个用户系统,每当创建新用户时,我希望它创建一个文件夹来存储所有进度(文件夹名称应该是他们输入的名称)。
例如:
从此:
到此(只需在游戏中输入用户名):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自此论坛:
From this forum:
在已知且可重现的路径2的子目录1内建立一个目录。
Establish a directory inside a sub-directory1 of a known and reproducible path2.
user.home
- which should be a directory that the game can read from and write to.定义“Java 应用程序的主目录”?没有这样的野兽。
出于安全考虑,您在执行此类操作时需要非常小心。最好在属性文件中定义一个目录并将其用作应用程序的基目录。然后,正如已经说过的,它只是使用
.mkdir{,s}()
来实现你想要的。@C.Reed 也正确地说,您应该检查 mkdir() 的返回值:Java 的文件 API 存在巨大缺陷,因为当目录/文件创建/移动时,它不会抛出异常。失败。幸运的是,Java 1.7 将通过其新的 API 解决这个问题。
(我遇到的一个例子是看到代码无法
.move()
周围的文件:问题是它在开发机器上工作,但在生产机器上目录将被移动另一个文件系统 --> 严重破坏)提示:使用 Apache 的 commons-io
Define the "main directory of your Java app"? There is no such beast.
You want to be very careful when doing things like this, for security concerns. Best to define a directory in a property file and use that as a base directory for your application. Then, as already said, it is just a use of
.mkdir{,s}()
to achieve what you want.And @C.Reed also rightly says that you should check for
mkdir()
's return value: Java's File API is hugely flawed in that it will not throw an exception when directory/file creation/move will fail. Fortunately, Java 1.7 will cure that with its new APIs.(an example I encountered is seeing code which would fail to
.move()
a file around: the problem is that it worked on the dev's machine, but on the production machine the directory was to be moved on another filesystem --> havoc)Hint: use Apache's commons-io
如果主目录是应用程序的工作目录,您可以使用以下命令创建一个目录
If the main directory is the working direct of your application you can create a directory with