user2412186
user2412186

Reputation:

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:\Users\raz\Documents\2pg.pdf (The filename, directory name, or volume label syntax is incorrect)

Upvotes: 0

Views: 2799

Answers (1)

drew moore
drew moore

Reputation: 32670

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

Upvotes: 2

Related Questions