Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am developing an application in Java. In one of my requirement, my application needs to create a folder and put excel files into it. The requirement is, suppose i have a folder and application is writing files to it and if the folder size increases spcified limit of suppose 5MB, the application needs to create new folder outside current folder, name it accordingly and start writng files into newly created folder. I have created following methods but somehow i am not able to get through this requirement.
Please help me with this. Thanks in advance!

Java
public String createDirectories(String strDirPath){
        File newDirectory;
        boolean isDirCreated = false;
        String newFilePath;
        int fileIdx=1;
        
        if (getFolderSize(new File(strDirPath)) <= 5242880){
             newFilePath = strDirPath;
             newDirectory = new File(newFilePath);
        } else {
             newFilePath = strDirPath+"\\"+strDirPath.substring(strDirPath.lastIndexOf("\\"))+"_"+fileIdx;//rename the folder depending upon name of parentfoler_[1,2,3,...]
             newDirectory = new File(newFilePath);
             fileIdx++;
        }
        
        
        try {
                if(!newDirectory.exists()){
                    
                    isDirCreated = newDirectory.mkdirs();
                }else
                    isDirCreated = true;
                
                                                  
                      
        } catch (SecurityException se){
            JOptionPane.showMessageDialog(null, se, "AccessDenied Error: Class-FileFactory", 0);
        }
        if (isDirCreated)
            return newDirectory.getPath() ;
        else
            return null ;
                  
    }

public  long getFolderSize(File directory) {
    long length = 0;
    
    for (File file : directory.listFiles()) {
        if (file.isFile())
            length += file.length();
        else
            length += getFolderSize(file);
    }
    
    return length;
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900