2013-11-21 83 views
1

我想獲取完整路徑和文件名。 我試圖如何獲取JFileChooser中的目錄?

package looks; 

import javax.swing.JFileChooser; 
import javax.swing.filechooser.FileNameExtensionFilter; 

public class Test { 

    public static void main (String[] args) { 
     JFileChooser chooser = new JFileChooser(); 
     FileNameExtensionFilter filter = new FileNameExtensionFilter(
      "CSV files", "csv"); 
     chooser.setFileFilter(filter); 
     int returnVal = chooser.showOpenDialog(chooser); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      System.out.println("directory and file : " + 
           chooser.getSelectedFile().getName() + 
           chooser.getAbsolutePath().getName()); 
     } 
    } 
} 

我的最終目標是讓這樣C:\\Users..\\Test\\myfile.csv繩子,讓我可以在代碼中使用它。

回答

3

試試這個:

public static void main (String[] args) { 
    JFileChooser chooser = new JFileChooser(); 
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
     "CSV files", "csv"); 
    chooser.setFileFilter(filter); 
    int returnVal = chooser.showOpenDialog(chooser); 
    if (returnVal == JFileChooser.APPROVE_OPTION) { 
     File file = chooser.getSelectedFile(); 
     System.out.println(file.getAbsolutePath()); 
    } 
} 
+0

感謝,作品〜!!!!!!!!!!!!!!! –