file io - Writing 'getSelectedFile' as a String (Java) -


    open.addactionlistener(new actionlistener(){         public void actionperformed (actionevent e){             jfilechooser filechooser = new jfilechooser();             filechooser.setcurrentdirectory(new java.io.file("."));             filechooser.setfileselectionmode(jfilechooser.directories_only);              if (filechooser.showopendialog(null) == jfilechooser.approve_option){                 system.out.println(filechooser.getselectedfile());                 }         }     }); 

this might badly worded question here go:

i need part of code produce 'filechooser.getselectedfile());' in format can used elsewhere. don't mind if variable (won't work because need call in actionlistener) or (as planned) output selected folder string output file , read file in elsewhere in program.

it's important file path (e.g. c:/users/desktop/) string because class use path takes in.

i've tried couple of options "inconvertable types' compile error etc, if has ideas share, that'd great

jfilechoose.getselectedfile() returns file object, not string object.

the file object has methods getabsolutepath(), getpath(), getname(), getparent() return string versions of file name , path.

so like:

file file = filechooser.getselectedfile(); system.out.println("selected file is: "+file.getabsolutepath()+"/"+file.getname()); 

should want.

also fyi, doesn't compile ...

string exportpath = filechooser.getselectedfile(); 

... because file object returned getselectedfile() not string object. however, file object (like objects) has tostring() method gets called automatically build string when did ...

string exportpath = filechooser.getselectedfile() +"\\"; 

the elegant way be, said, this:

string exportpath = filechooser.getselectedfile().getabsolutepath(); 

hope helps, luck! rob


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -