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を使用)を渡すと、任意の深さのディレクトリ構造になります。一度に1つのディレクトリにパスを作成する必要はありません。ディレクトリがすでに存在する場合、またはそれらの一部が存在する場合でも、期待どおりに機能します。

于 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はIONewIOを含む非常に多くの拡張機能を導入しました。

相互に複数のディレクトリを作成する場合は、1.7以降で使用可能なFiles.createDirectoriesを利用できます。最初に存在しないすべての親ディレクトリを作成することにより、ディレクトリを作成します。

于 2016-02-26T19:58:02.027 に答える