Search code examples
javafxcomboboxappendmkdirs

How to append more than two folders to user.home java


guys! I'm trying to append folder to user.home property. It working nice, while I'm using just one additional folder. But when I try to make another two additions (so it looks like user.home+folder1+folder2+folder3) it trows --- java.lang.IllegalArgumentException: Folder parameter must be a valid folder--- . My though that there is some restriction, but can't find out where.

    String fullRoute = null;
    File homeDir = new File("MLog");
    if (!SiteCo.getEditor().getText().isEmpty() &&         
    !InciDate.getEditor().getText().isEmpty()) {
            homeDir.mkdirs();

      fullRoute = System.getProperty("user.home") + File.separator + 
     //SaveVarTo.getLastVisitedDirectory() +
                    SaveVarTo.AddPath(SiteCo.getValue().toString()) + 
     File.separator + SaveVarTo.AddPath(InciDate.getValue().toString());
        }
        else {homeDir.mkdirs();
    //   File.separator+homeDir.toString() - without it         
    fullRoute = 
    System.getProperty("user.home")+File.separator+homeDir.toString();}
                System.out.println(fullRoute);

            fileChooser.setInitialDirectory(new File(fullRoute));
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("XML Files", "*.xml"));


//sample of method
public class Variables{
public String AddPath(String name) {

        if (!name.isEmpty()) {
            //File nou = new File(getLastVisitedDirectory() +"\\" + name);

            File nou = new File(name);
            if (!nou.exists()) {
                nou.mkdirs();

            } else {
                System.out.println("Folder already exists");
            }


        }
        else{name = null;}
        return name;
    }}

Solution

  •  String fullRoute = null;
        File homeDir = new File(System.getProperty("user.home"));
    
        if (SiteCo.getValue() !=null && InciDate.getValue() !=null) {
    
            System.out.println(SaveVarTo.getMainFolder());
            fullRoute = homeDir + File.separator + SaveVarTo.getMainFolder() +
                    File.separator + SiteCo.getValue().toString() + 
        File.separator + InciDate.getValue().toString();
    
        } else {
            fullRoute = homeDir.toString();
        }
    
        System.out.println(fullRoute);
        File fhd = new File(fullRoute);
        if (!fhd.exists())
            fhd.mkdirs();
        fileChooser.setInitialDirectory(fhd);
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("XML Files", "*.xml"));