import java.io.*; import javax.swing.*;
public class filepath { public static void main(String args[]) { String currentFilePath = null; File file = null; String currentFileName = null; JFileChooser jFileChooser = new JFileChooser(); int result = jFileChooser.showOpenDialog(null); if(result == JFileChooser.APPROVE_OPTION) { currentFilePath = jFileChooser.getSelectedFile().getPath(); try { file = new File(currentFilePath); currentFileName = file.getAbsolutePath();//完整路径 System.out.println(currentFileName); currentFileName = file.getParent();//父路径,也是完整的,你可以通过StringTokenizer分解开来 System.out.println(currentFileName); // ...your code } catch(NullPointerException npe) { String errorMsg = new String("status: Error by opening " ); System.err.println(errorMsg); } /*catch(IOException ioe) { System.err.println(ioe); }*/ } }