Question

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.

Était-ce utile?

La solution

It is recommended to use the mkdirs method of the File class instead of checking multiple status flags when creating nested directories. Also, never use concatenation for creating File 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:

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

}

 

Autres conseils

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.

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

Output

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

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."

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top