Link to home
Start Free TrialLog in
Avatar of sitijaafar
sitijaafarFlag for Malaysia

asked on

Have a problem when create folder

Hi experts,
I'm having a problem in creating a folder.  It failed create a folder.  I have tried with two folder:
A) C:/apache-tomcat-6.0.18/webapps/grid50/grid_archive
B) C:/apache-tomcat-6.0.18/webapps/grid50/grid_archive/ram/0

For create (A) folder it's doesn't have a problem.  But when I create (B) folder it's failed. Below is the code.
public String createTempDir2(int module, int mid) throws Exception{
		boolean bGotFolder = true;
		String tempPath = null;
		String tempdir = null;
		
		try{
			
			tempdir = System.getProperty("archive_path");			
			
			if(module == 1)
			{
				tempPath = tempdir+"/erm"+"/"+mid;
			}
			else if(module == 2 )
			{
				tempPath = tempdir+"/ram"+"/"+mid;			 
			}		
			
		    if(log.isDebugEnabled())
		    	log.debug("Default temp file path = " + tempPath);
		    
		    File folderExisting = new File(tempPath);
		    
			if (!folderExisting.isDirectory() || !folderExisting.exists())
			{
				if(!folderExisting.mkdir())
				{
			    	if(!folderExisting.isDirectory())
			    	{	    		
			    		
			    		if(log.isDebugEnabled())
			    			log.debug("Failed to create temp because a file with same name exists. ");
			    		
			    		if(folderExisting.delete())
			    		{
				    		if(!folderExisting.mkdir())
				    		{
								bGotFolder = false;
								
								if(log.isDebugEnabled())
									log.debug("Failed to create temp");
				    		}
				    		else
				    		{
				    			if(log.isDebugEnabled())
									log.debug("Created new dir after deleting file, name = " + folderExisting.getPath());				    			
				    		}
			    		}
			    		else
			    		{
							bGotFolder = false;
							
				    		if(log.isDebugEnabled())
				    			log.debug("Failed to delete the file.");			    			
			    		}
			    	}
			    	else
			    	{
						bGotFolder = false;
						
			    		if(log.isDebugEnabled())
			    			log.debug("Failed to create temp folder.");
			    	}
				}
				else
				{
				    if(log.isDebugEnabled())
				    	log.debug("Created new dir, name = " + folderExisting.getPath());					
				}		
				
			}			
								
		}
		catch(Exception e)
		{
		    log.error("function = createTempDir2(); desc = "+e);		    
		}

		return tempPath;
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sitijaafar

ASKER

it's work. So, in what condition to know whether to use mkdir or mkdirs?
whether the parent directory exists or not