2016-06-28 107 views
1

你好,這已經有一段時間了。所以我正在研究這個應用程序,它允許我創建和保存文件等等等等等等。這不是這個問題的重點。所以在保存功能中,我決定使用JFileChooser來允許用戶選擇保存文件的位置。爲什麼JFileChooser以這種方式工作?

現在我的JFileChooser正常啓動,這裏是代碼:

public void Save() { 
     choicer.setCurrentDirectory(new java.io.File("C://")); 
     choicer.setDialogTitle("Save Document"); 
     choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
     choicer.setAcceptAllFileFilterUsed(false); 
     if (choicer.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) { 
      dir = String.valueOf(choicer.getCurrentDirectory()); 
     } 

     File f = new File(dir + "\\hi.txt"); 
    } 

忽略hi.txt名。我將在稍後重點討論。

現在有2個問題,我找到了。首先,我必須「深入」一個文件夾來保存我的文件。 說明:
讓我們說我要救我的文件中公開的用戶,我的目錄是這樣的:

C:\用戶\公共
現在我的代碼添加\ hi.txt在文件參數,不應該保存在公共文件夾中。如果我只是開上市後保存在那裏,我得到一個異常:

java.io.FileNotFoundException: C:\Users\Public (Access is denied) 
at java.io.FileOutputStream.open0(Native Method) 
at java.io.FileOutputStream.open(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileWriter.<init>(Unknown Source) 
at Spreadr$TableMethods.Save(Spreadr.java:175) 
at Spreadr.lambda$1(Spreadr.java:85) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.AbstractButton.doClick(Unknown Source) 
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) 
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

如果我然而,在公衆打開一個文件夾,然後點擊保存,我的目錄現在的樣子:

C:\用戶\ Public \ Downloads

然後它不會保存在Downloads文件夾中,而是保存在Public User文件夾中。

這是我的第一個問題。我的第二個實際上是一個迷你問題類型的東西。我可以在保存菜單(JFileChooser菜單)中命名我的文件嗎?

+2

得到你想要使用'getCurrentDirectory'我不認爲,嘗試'getSelectedFile',構建最終的文件還當,你可以使用兩個參數版本的文件。 '新文件(dir,「hi.txt」);' – matt

回答

2

代碼調用choicer.getCurrentDirectory(),它會給你的你選擇在JFileChooser.DIRECTORIES_ONLY模式下的目錄時,在哪個目錄的父目錄。

你可能希望得到您所選擇的目錄中,您通過調用

choicer.getSelectedFile() 
相關問題