在 user.home 的另一个目录中创建一个目录?
我只是想知道我编写的代码是否可以在彼此之间创建多个目录。我使用这个作为参考。
String username = enterUserTF.getText(); //the username the user enters in a textfield.
boolean myGamesFolderSuccess = new File(System.getProperty("user.home"), "My Games").mkdir();
boolean mainFolderSuccess = new File("My Games", "Type King").mkdir();
boolean userSuccess = new File("TypeKing", username).mkdir(); //creates a folder with the users username.
if(myGamesFolderSuccess){
if(mainFolderSuccess){
if(userSuccess){
System.out.println("Directory " + username + " created.");
File f = new File(username + "/test.txt");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Could not create user's file.");
}
}
}
}
}
}
综上所述,我在 user.home
中创建了第一个目录“My Games”,然后将我的游戏名称“Type King”放入该目录中,每当用户输入用户名时,我想要创建一个目录作为他们的用户名。 File f
仅检查 username
目录中的文件。
I was just wondering if the code I made will work to create multiple directories within each other. I used this as a reference.
String username = enterUserTF.getText(); //the username the user enters in a textfield.
boolean myGamesFolderSuccess = new File(System.getProperty("user.home"), "My Games").mkdir();
boolean mainFolderSuccess = new File("My Games", "Type King").mkdir();
boolean userSuccess = new File("TypeKing", username).mkdir(); //creates a folder with the users username.
if(myGamesFolderSuccess){
if(mainFolderSuccess){
if(userSuccess){
System.out.println("Directory " + username + " created.");
File f = new File(username + "/test.txt");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Could not create user's file.");
}
}
}
}
}
}
So to sum up the above, I made the the first directory "My Games" in user.home
, then placed my game's name, "Type King" in that directory, and whenever the user enters a username, I want a directory to be created that is their username. File f
just checks for a file in the username
directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
建议在创建嵌套目录时使用
File
类的mkdirs
方法,而不是检查多个状态标志。另外,切勿使用串联来创建File
对象/路径。另外,如果您希望游戏可移植,请确保目录名称中没有特殊字符,例如空格等。为什么要向用户询问名称而不是从
user.name系统属性? 的东西应该有效
像这样
:
It is recommended to use the
mkdirs
method of theFile
class instead of checking multiple status flags when creating nested directories. Also, never use concatenation for creatingFile
objects/paths.Also, if you want your game to be portable, make sure you don't have special characters in your directory names like a space etc.Why are you asking user for the name instead of retrieving it from
user.name
system property? Something like this should work:}
如果将完整路径传递给 File.mkdirs(带有 s),它将创建任意深度的目录结构。您不必一次为一个目录构建路径。如果目录已经存在,或者其中一些目录存在,它仍然会按您的预期工作。
If you pass a full path to File.mkdirs (with an s) it will make an arbitrarily deep directory structure. You don't have to build paths one directory at a time. If the directories already exist, or if some of them exist, it will still work as you expect.
输出
Output
我认为最好使用 API 中现有的功能。如果没有任何限制,请考虑切换到最新的 JDK。在 1.7 中,Oracle 确实引入了很多增强功能,包括 IO 和 New IO。
要在彼此之间创建多个目录,您可以利用 Files.createDirectories 自 1.7 起可用。 “它将通过首先创建所有不存在的父目录来创建一个目录。”
I think its better to use existing functionality available in the API. If you don't have any restrictions consider switching to the latest JDK. In 1.7 Oracle did introduce so many enhancements including IO and New IO.
For creating multiple directories within each other you can take advantage of Files.createDirectories available since 1.7. "It will create a directory by creating all nonexistent parent directories first."