I am using JFileChooser to get a directory path in a project of mine. It is working perfectly, but there is a little problem. Suppose this is the directory structure:
->Home
->Documents
->Java
This is the code:
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int userSelection=fileChooser.showSaveDialog(this);
if(userSelection==JFileChooser.APPROVE_OPTION){
try{File fileTosave=fileChooser.getSelectedFile();
File newFile=new File(fileTosave.getAbsolutePath()+"satish.txt");
System.out.println(newFile);
this.dispose();}
catch(Exception e){}
}
If currently i am inside the java folder, it gives me the path Home/Documents/Java
(or Home:\Documents\Java) in windows. What I want is that it should return the path which includes single forward slash or double forward slash (according to plaform) such that it looks like Home/Documents/Java/
. I want to do this because later I have to append a file name to this path such that file path becomes Home/Documents/java/file.txt
.
Any Idea on how to do this?
I don't want to add slashes manually because then I would also have to keep the platform in mind.
Thank you!