如何在 Java 应用程序的根目录中创建文件夹?

发布于 2024-12-23 18:54:58 字数 296 浏览 4 评论 0 原文

有没有办法在我的 Java 应用程序的主目录中创建一个文件夹(具有特定名称)。我的游戏中有一个用户系统,每当创建新用户时,我希望它创建一个文件夹来存储所有进度(文件夹名称应该是他们输入的名称)。

例如:

从此:

到此(只需在游戏中输入用户名):

在此处输入图像描述

Is there a way to create a folder (with a specific name) in the main directory of my Java app. I have a user system in my game, and whenever a new user is created, I want it to create a folder that will store all there progress in (and the folder name should be the name they put in).

For example:

From this:

enter image description here

To this (just by entering a username in the game):

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒浓于脸红 2024-12-30 18:54:58

来自此论坛

File f = new File("TestDir");
try{
  if(f.mkdir())
    System.out.println("Directory Created");
  else
    System.out.println("Directory is not created");
}catch(Exception e){
  e.printStacktrace();
}

From this forum:

File f = new File("TestDir");
try{
  if(f.mkdir())
    System.out.println("Directory Created");
  else
    System.out.println("Directory is not created");
}catch(Exception e){
  e.printStacktrace();
}
倒带 2024-12-30 18:54:58

在已知且可重现的路径2的子目录1内建立一个目录。

  1. EG 基于主类的包名称的目录结构 - 这有助于避免与其他应用程序发生冲突。
  2. user.home 是一个好地方 - 它应该是游戏可以读取和写入的目录。

Establish a directory inside a sub-directory1 of a known and reproducible path2.

  1. E.G. a directory structure based on the package name of the main class - this helps avoid collisions with other apps.
  2. A good place is user.home - which should be a directory that the game can read from and write to.
[浮城] 2024-12-30 18:54:58

定义“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

久伴你 2024-12-30 18:54:58

如果主目录是应用程序的工作目录,您可以使用以下命令创建一个目录

new File("new-dir").mkdir();

If the main directory is the working direct of your application you can create a directory with

new File("new-dir").mkdir();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文