0

我只是想知道我制作的代码是否可以在彼此之间创建多个目录。我用这个作为参考。

        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.");
                            }
                        }
                    }   
                }
            }
        }

综上所述,我在 中创建了第一个目录“My Games” user.home,然后将我的游戏名称“Type King”放在该目录中,每当用户输入用户名时,我都希望创建一个目录,即他们的用户名。 File f只检查目录中的文件username

4

4 回答 4

2

如果您将完整路径传递给 File.mkdirs(带有 s),它将创建一个任意深度的目录结构。您不必一次构建一个目录的路径。如果这些目录已经存在,或者其中一些存在,它仍然可以按您的预期工作。

于 2012-01-04T05:44:03.073 回答
2

建议在创建嵌套目录时使用类的mkdirs方法,File而不是检查多个状态标志。此外,切勿使用串联来创建File对象/路径。

此外,如果您希望您的游戏可移植,请确保您的目录名称中没有特殊字符,例如空格等。为什么要向用户询问名称而不是从user.name系统属性中检索它?像这样的东西应该工作:

String username = System.getProperty("user.name");
File myGamesDir = new File(System.getProperty("user.home"), "my-games");
File typeKingDir = new File(myGamesDir, "type-king");
File userDir = new File(typeKingDir, username);
boolean userSuccess = userDir.mkdirs();
if(userSuccess){
    System.out.println("Directory " + username + " created.");
    File f = new File(userDir, "test.txt");
    if(!f.exists()){
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Could not create user's file.");
        }
    }

}

 

于 2012-01-04T05:44:45.740 回答
1
import java.io.File;
import javax.swing.JOptionPane;

class Dirs {

    public static void main(String[] args) throws Exception {
        String subDir = "My Games|Type King";
        String userName = JOptionPane.showInputDialog(
            null,
            "Who are you?");
        subDir += "|" + userName;
        String[] parts = subDir.split("\\|");
        File f = new File(System.getProperty("user.home"));
        for (String part : parts) {
            f = new File(f, part);
        }
        boolean madeDir = f.mkdirs();
        System.out.println("Created new dir: \t" + madeDir  + "  \t" + f);

        f = new File(f, "eg.txt");
        if (!f.exists()) {
            boolean madeFile = f.createNewFile();
            System.out.println(
                "Created new file: \t" + madeFile  + "  \t" + f );
        }
    }
}

输出

Created new dir:        true    C:\Users\Andrew\My Games\Type King\BilboBaggins
Created new file:       true    C:\Users\Andrew\My Games\Type King\BilboBaggins\eg.txt
于 2012-01-04T05:59:36.637 回答
0

我认为最好使用 API 中可用的现有功能。如果您没有任何限制,请考虑切换到最新的 JDK。在 1.7 中,Oracle 确实引入了许多增强功能,包括IONew IO

为了在彼此之间创建多个目录,您可以利用自 1.7 以来可用的Files.createDirectories 。"它将通过首先创建所有不存在的父目录来创建一个目录。 "

于 2016-02-26T19:58:02.027 回答