-3

I have a method which takes in the parameters such as hostName, userName. I create a fileName by adding the recent time to it. I want to create a file in the system, which includes the format /hostName/userName/fileName so that the user who logs in, creates a file in their own directory with their own credentials, the user directory contains their domain and username. Inside the directory /hostName/userName is a file with the name fileName. EG:

User1: /windows/user1/201512110309.txt

User2: /ubuntu/user2/201512111210.txt

   public File getDirectoryAndFileName(String hostName, String userName) {
                    String fileName = new SimpleDateFormat("yyyyMMddhhmm'.txt'").format(new Date());
                    File file = new File(hostName + userName + fileName);
                    return file;
    }
Mykola
  • 3,343
  • 6
  • 23
  • 39
YourAboutMeIsBlank
  • 1,787
  • 3
  • 18
  • 27
  • Possible duplicate of [How to create a directory in Java?](http://stackoverflow.com/questions/3634853/how-to-create-a-directory-in-java) – Sam Berry Nov 12 '15 at 20:22
  • `Files.createDirectories(Paths.get(hostname, userName))` should do. – aioobe Nov 12 '15 at 20:22
  • @SamB. How is it a possible duplication? I am creating a directory from a string variable, that too multiple variables arguments passed into the method. And I am even creating a new string name and adding that to the path name that too a variable and finally passing it as a path name to the File constructor. – YourAboutMeIsBlank Nov 12 '15 at 20:25
  • 1
    Essentially it is exactly what Sam. B posted just instead of using a hard set you would use the variable that you created to do this. – basic Nov 12 '15 at 20:32

2 Answers2

0

You can do something like this:

public File createFileStructure(hostName, userName) {
  File baseUserDirectory = new File(hostName, userName);
  String userFileNameToBeCreated = "yyyyMMddHHmm.txt";
   if(!baseUserDirectory.exists()) {
     if(!baseUserDirectory.mkdirs()) {
         throw new IllegalStateException("The base user directory does not exist and could not be created: "
                   + baseUserDirectory);
       }
   }
   if(baseUserDirectory.isDirectory ()) {
     throw new IllegalStateException("The base user directory is not a directory: " + baseUserDirectory);
   }
   //till here you will be sure that your user Directory exists.
   return new File(baseUserDirectory, userFileNameToBeCreated);
  }

You just have to get the date in the format you want.

0

Thank you @recursingMe. I implemented in the following way.

public static void getFile(String hostName, String userName) throws IOException {
            hostName = "/" + hostName;
            userName = "/" + userName;
            File baseUserDirectory = new File(hostName, userName);
            baseUserDirectory.mkdirs();
            String fileName = new SimpleDateFormat("yyyyMMddhhmm'.txt'").format(new Date());        
            File file = new File(baseUserDirectory, fileName);
            file.createNewFile();
        }
YourAboutMeIsBlank
  • 1,787
  • 3
  • 18
  • 27