How to save path of a file
Hello,
As i am very new to programing i wasn't sure where to post this question.
I have a program to open a file. Doesnt do anything at the moment, What i need to know is how to save the "PATH" of the file to a variable to use later. THis is what i have right now, not sure how to go about this.
Code:
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("dir"));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.showOpenDialog(frame);
}
Re: How to save path of a file
If you have a File object you will always be able to access its Path.
Re: How to save path of a file
I have figured it out somewhat. Still need help though. This is only returning the Current Directory. what i need is the full PATH. As in c:\folder\folder\file.txt
Code:
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("dir"));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.showOpenDialog(frmndVirtualFighter);
//File selFile = chooser.getSelectedFile();
File selFile = chooser.getCurrentDirectory();
String pathName;
pathName = chooser.getName(selFile);
JOptionPane.showMessageDialog(frame, pathName, "About", JOptionPane.PLAIN_MESSAGE);
Re: How to save path of a file
As Junky suggests, use the JFileChooser to select the File, but not to get the path String. Instead call one of the several available File methods on the chosen file, here selFile, to get the path String, methods such as getCanonicalPath() or one of the several others that you can find on the File API.
Re: How to save path of a file
Thank you guys for your quick response. getCanoniclePath() worked perfectly