Changing the directory path’s forward slash to backslash

Tags: ,

I’m using the JFile chooser, and trying to import a pdf file but;

if (option == JFileChooser.APPROVE_OPTION) {
    String fs = File.separator;
     String filelist = " "; 
     filelist = " "+chooser.getSelectedFile();
     filelist = filelist.replace("\","/");
     File sf = new File(filelist);

 statusbar.setText("You chose " + filelist);

 PDDocument doc = null;
         filelist = filelist.replace("\","/");

    doc = PDDocument.load(filelist);

System.out.println(filelist); perfectly prints the desired outcome with forward slashes;


but the doc gives an error with backslashes;
C:UsersrazDocuments2pg.pdf (The filename, directory name, or volume label syntax is incorrect)


It’s not the path separator that’s causing your problem, its the space at the front of the name:

filelist = " "+chooser.getSelectedFile();

It should just be:

filelist = chooser.getSelectedFile();

Source: stackoverflow