Re: Obtain file path and file name using file chooser
bH wrote:
Andrew Thompson wrote:
bH wrote:
I want to obtain both file "path" and the filenamed MyText.txt ....as
in "C:\Documents
and Settings\bH\Desktop\MyShowWithFrame\MyText.txt"
....
....
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile ();
strngFileName = file.getName();
....
strngFileName = file.getPath() +
System.getProperty("file.separator") +
file.getName();
...
Because I am connecting with a printer after locating a file address
when I am using the file chooser, all the file separators are "\" but
should be "/".
Aaaah.. my original answer was ..incomplete.
I was prodding you in the direction (I thought) this
code needs to go, but since we've already arrived 'there',
let's take it a bit further.
1) For starters, the way I constructed that file was
sloppy and inefficient. The *best* way to create a File
in a given directory is to use the File contstructor to
create a File based on ..
new File(File parent, File theFile)
...to automatically get the correct separator.
2) I cannot see why the code needs to be manipulating the
String name of the File(s) at all. Don't the later methods
accept files as parameters? If that is the case, you can
simply hand the later methods 'the file', and be done with it.
3) If the String name of the File *is* actually of interest, I
am pretty sure that File has some methods to produce
the entire path and filename. Check the API..
Andrew T.