-1

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vipul Tyagi
  • 547
  • 3
  • 11
  • 29
  • 1
    Does this answer your question? [Path delimiter in windows and linux for java code](https://stackoverflow.com/questions/44250017/path-delimiter-in-windows-and-linux-for-java-code) – Tom May 02 '20 at 16:49
  • Why there is a downvote? Didn't get that – Vipul Tyagi May 02 '20 at 17:59

2 Answers2

1

Use java.io.File.pathSeparator

/**
     * The system-dependent path-separator character, represented as a string
     * for convenience.  This string contains a single character, namely
     * <code>{@link #pathSeparatorChar}</code>.
     */
    public static final String pathSeparator = "" + pathSeparatorChar;

Your code should look like

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.getParent() + File.separator +"satish.txt");
                System.out.println(newFile);
                this.dispose();}
            catch(Exception e){}
        }
QuickSilver
  • 3,915
  • 2
  • 13
  • 29
0

Java doesn't provide a method or strategy to convert file paths as Strings to the format for different OS's out of the box. You would need to either write your own method to do this or take advantage of a library that has already solved this for you.

For example, you can use Apache Commons IO to solve this, using the method:
FilenameUtils.separatorsToSystem(String path)
pczeus
  • 7,709
  • 4
  • 36
  • 51