Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create intermediate folders if one doesn't exist

Tags:

java

file

I am trying to create a folder for each username a user logs in as. Currently I have

private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads File theFile = new File(destination + username); // will create a sub folder for each user  

but the File theFile bit does not create a new folder for the username. How would I do this ?

I have tried

private String destination;  public void File()  {     destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads     File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution)      theFile.mkdirs(); } 

but I need to use the destination later on in the program, how would I do that?

This is my whole code:

/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package richard.fileupload;  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.context.FacesContext; import java.io.File; import org.primefaces.event.FileUploadEvent;  @ViewScoped @ManagedBean(name = "fileUploadController") public class FileUploadController {      /*      public void handleFileUpload(FileUploadEvent event) {      System.out.println("called");      FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");      FacesContext.getCurrentInstance().addMessage(null, msg);      }      }      */     private String username;     private String destination;      @PostConstruct     public void init() {         System.out.println("called get username");         username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();     }      public void File() {     destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads     File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution)      theFile.mkdirs(); }      public File getDirectory(String destination, String username) {         System.out.println("called get directory");         // currently not working, is not calling the username or destination          //set the user directory from the destinarion and the logged user name         File directory = new File(destination, username);         //check if the location exists         if (!directory.exists()) {             //let's try to create it             try {                 directory.mkdir();             } catch (SecurityException secEx) {                 //handle the exception                 secEx.printStackTrace(System.out);                 directory = null;             }         }         return directory;     }      public void handleFileUpload(FileUploadEvent event) {         System.out.println("called handle file");         FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); //Displays to user on the webpage         FacesContext.getCurrentInstance().addMessage(null, msg);         try {             copyFile(event.getFile().getFileName(), event.getFile().getInputstream());         } catch (IOException e) {             //handle the exception             e.printStackTrace();         }     }      public void copyFile(String fileName, InputStream in) {         try {               // write the inputStream to a FileOutputStream             OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm             System.out.println("Called CopyFile"); //testing              System.out.println(destination + fileName);              int read = 0;             byte[] bytes = new byte[1024];              while ((read = in.read(bytes)) != -1) {                 out.write(bytes, 0, read);             }              in.close();             out.flush();             out.close(); //make sure new file is created, (displays in glassfish server console not to end user)             System.out.println("New file created!");//testing         } catch (IOException e) {             e.printStackTrace();              FacesMessage error = new FacesMessage("The files were not uploaded!");             FacesContext.getCurrentInstance().addMessage(null, error);         }     } } 

FINAL EDIT (Hopefully)

 public void copyFile(String fileName, InputStream in) {         try {              destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads             File theFile = new File(destination + "/" + username);              theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK)              System.out.println("Completed File");             // write the inputStream to a FileOutputStream             OutputStream out = new FileOutputStream(new File(destination + fileName)); // cannot find path when adding username atm             System.out.println("Called CopyFile"); //testing              System.out.println(destination + fileName);              int read = 0;             byte[] bytes = new byte[1024];              while ((read = in.read(bytes)) != -1) {                 out.write(bytes, 0, read);             }              in.close();             out.flush();             out.close(); //make sure new file is created, (displays in glassfish server console not to end user)             System.out.println("New file created!");//testing         } catch (IOException e) {             e.printStackTrace();              FacesMessage error = new FacesMessage("The files were not uploaded!");             FacesContext.getCurrentInstance().addMessage(null, error);         }     } } 

Just how can i print out the new destination and use this later on as currently it creates the new folder but does not select it to use

EDIT SOLVED THIS TOO :

    NewDestination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/" + username; 

Added the above code and now it all works

like image 668
user1924104 Avatar asked Feb 02 '13 20:02

user1924104


People also ask

How do you create a directory if it does not exist in Java?

You can use the Java File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose. The mkdir() method creates a single directory if it does not already exist.

How do you create a folder if it doesn't exist PowerShell?

PowerShell Create Directory If Not Exists using Test-Path If a directory exists then it will return $True. If a path or directory is missing or doesn't exist, it will return $False. Using PowerShell New-Item cmdlet, it will create directory if not exists using Test-Path.

How do you create a directory if it doesn't exist in bash?

When you want to create a directory in a path that does not exist then an error message also display to inform the user. If you want to create the directory in any non-exist path or omit the default error message then you have to use '-p' option with 'mkdir' command.


1 Answers

You have to actually call some method to create the directories. Just creating a file object will not create the corresponding file or directory on the file system.

You can use File#mkdirs() method to create the directory: -

theFile.mkdirs(); 

Difference between File#mkdir() and File#mkdirs() is that, the later will create any intermediate directory if it does not exist.

like image 84
Rohit Jain Avatar answered Sep 23 '22 22:09

Rohit Jain