Skip to content
Advertisement

Changing the directory path’s forward slash to backslash

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);
 System.out.println(filelist);

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

    doc = PDDocument.load(filelist);

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

C:/Users/raz/Documents/2pg.pdf

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

Advertisement

Answer

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();

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement