samedi 4 avril 2015

Unexpected interaction with String and File

I have following pieces of code:



if (e.getSource() == theView.addButton) {

System.out.println("Add Button clicked");
theView.setBotTextArea("Adding category...");

File directory = new File(theModel.getDirectory() + theView.getCategoryNameInput());

boolean isDirectoryCreated = directory.mkdir();

if(isDirectoryCreated) {

System.out.println("Created new directory in: " + directory);

} else if (directory.exists()) {

System.out.println("Category already exists!");

}

}


This is part of the ActionListener's ActionPerformed() method.



private File directory = new File("C:/Users/Lotix/Desktop/TestFolder/");
public File getDirectory() {

return directory;

}


What i expect this method to do is to create a subfolder in the chosen directory. However, for some reason unknown to me, it creates completly another folder on my desktop instead of TestFolder.


I tried theModel.getDirectory().toString() and manipulating the variable but to no avail. The solution i came up with is to simply add forward slash between theModel.getDirectory() and theView.getCategoryNameInput() such as this:


File directory = new File(theModel.getDirectory() + "/" + theView.getCategoryNameInput());


However, when i concatenate File variable with another String it works perfectly fine.


What gives?


Aucun commentaire:

Enregistrer un commentaire