2016-05-02 16 views
9

tôi cần phải sử dụngJava file.getPath() trả về ngôn ngữ biến đổi con đường

fileChooser.getSelectedFile() 

phương pháp tuy nhiên nó luôn luôn trả về ngôn ngữ biến đổi con đường bởi vì một số thư mục được dịch trong OSX. Ví dụ thư mục "/ Downloads" được dịch sang ngôn ngữ hệ thống của tôi "/ Stiahnuté" nhưng con đường thực sự là "/ Downloads"

trở lại:

/Users/John/Stiahnuté 

vọng

/Users/John/Downloads 

Nếu tôi chọn một số sub-directory sau đó fileChooser.getSelectedFile() trả lại đường dẫn bên phải một lần nữa. Có vẻ rằng luôn luôn chỉ mục cuối cùng trong đường dẫn được dịch

/Users/John/Downloads/subDirectory 

Code:

saveButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
       JFileChooser fileChooser = new JFileChooser(); 
       fileChooser.setFileFilter(new FolderFilter()); 
       fileChooser 
         .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
       if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { 
        File selectedFile = fileChooser.getSelectedFile(); 
        System.out.println("save path: " 
          + selectedFile.getPath()); 
        doSomething(selectedFile); 
       } 
      } 
     }); 

UPDATE:

tôi đã thực hiện ít workaround nhưng nó không phải là giải pháp hoàn hảo. Tuy nhiên nó làm việc cho tôi.

JFileChooser fileChooser = new JFileChooser(); 
       FileNameExtensionFilter filter = new FileNameExtensionFilter(
         "Directories", "dir"); 
       fileChooser.setFileFilter(filter); 
       if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { 
        File selectedFile = fileChooser.getSelectedFile(); 
        File newDir = new File(selectedFile.getPath()); 
        if (!newDir.exists()) { 
         newDir.mkdir(); 
        } 
        doSomething(); 
       } 
+1

Tôi sẽ thử 'fileChooser.getSelectedFile(). GetCanonicalFile()'. Nếu điều đó không hiệu quả, hãy thử sử dụng lớp Path hiện đại hơn: 'fileChooser.getSelectedFile(). ToPath(). ToRealPath(). ToFile()' – VGR

+0

không may java.nio.file.NoSuchFileException:/Users/John/Stiahnuté – Matwosk

Trả lời

1

Tôi có thể tạo lại sự cố trên Mac OS X 10.11.4 với Java 1.8.0_66. Đối với tôi, điều này trông giống như một lỗi (hoặc ít nhất là hành vi không mong muốn) trong việc thực hiện JFileChooser. Bạn có thể mở một báo cáo lỗi cho vấn đề này.

Với sự giúp đỡ của một answer explaining to use FileDialog to get a operating system native file chooseranswer about using it to select directories khác tôi thấy cách giải quyết như sau:

final Frame parent = …; // can be null 

System.setProperty("apple.awt.fileDialogForDirectories", "true"); 
final FileDialog fileDialog = new FileDialog(parent); 
fileDialog.setVisible(true); 
System.setProperty("apple.awt.fileDialogForDirectories", "false"); 

final File selectedDirectory = new File(fileDialog.getDirectory(), fileDialog.getFile()); 
System.out.println(selectedDirectory); 
System.out.println(selectedDirectory.exists()); 

Lưu ý rằng việc sử dụng "apple.awt.fileDialogForDirectories" là, tất nhiên, nền tảng cụ thể và sẽ không hoạt động trên hệ điều hành khác.

Các vấn đề liên quan