As the question states, I am trying to get a Path value, using JFileChooser. The returns from JFileChooser present me with two problems. First, the exact nature of what I am doing involves letting the user choose a directory, then, renaming all files of a certain type, for instance, all .pdf files, within that folder while leaving all other files alone. The problems I am having with JFileChooser in this regard are two-fold:
1) I have figured out how to let JFileChooser show the files inside a directory, but not allow the user to choose a file specifically. However, the return value of the string does not include the currently viewed directory inside JFileChooser when “Open” is pushed. For instance, if a user navigates to C:\Documents and Settings\myFolder\Documents\Work and sees the contents of the Work directory, then presses “Open”, the String returned as the getAbsolutePath() call is “C:\Documents and Setting\myFolder\Documents” the “Work” is never returned. How do I get that to be presented in the final outcome?
2) Once I have that String, how do I convert that to a Path so that I can operate on the files within it? The “\” is Java’s escape, and to use the file path as a valid Path value it needs to be escaped, i.e. “C:\Documents and Settings\myFolder” doesn’t work, it has to be “C:\Documents and Settings\myFolder”. How do I get from the single slash () return String from JFileChooser to the double slash (\) Path I need?
I know that the answer is likely simple, I am just more used to dealing with Linux, where this is not a problem. I have just landed my first job using my programming skills, and of course, everything is Windows.
And before someone suggests, yes, I know using “/” would work for relative paths, but for the application I am writing, I need to use absolute paths as these files could be on a host machine, or a network drive, so I can’t use relative naming, unless I am overlooking something due to not having a whole lot of network programming experience.
I thank you all for your time in reading, if not responding to this.
Tommie Matherne
1. For getting the whole path of the selected directory do this:
Here
myJfcis aninstance of JFileChoooserhere2. Conver the String into a path using this:
Use “\\” instead of “\” in the path.
ie.
C:\\Documents and Settings\\myFolderPlease see that you have proper Privilege in the C: drive else your file will not be created there, if you Do Not have the privilege to create a file in C: drive, try some other drive which you have like D:, E: etc.. You wont have any Privilege problem there.