为什么JFileChooser以这种方式工作?

时间:2016-06-28 07:11:25

标签: java 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个问题。首先,我必须“深入”一个文件夹来保存我的文件。的说明:
我们假设我要将文件保存在Public in Users中,我的目录如下:

C:\用户\公共
现在我的代码在文件参数中添加 \ hi.txt ,不应该保存在Public文件夹中。如果我在公开场合后保存在那里,我会得到一个例外:

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:\用户\公共\下载

然后它不会保存在Downloads文件夹中,而是保存在Public User文件夹中。

这是我的第一个问题。我的第二个实际上是一个迷你问题类型的东西。我可以在保存菜单(JFileChooser菜单)中命名我的文件吗?

1 个答案:

答案 0 :(得分:2)

您的代码调用choicer.getCurrentDirectory(),它将为您提供在JFileChooser.DIRECTORIES_ONLY模式中选择目录时所在目录中的父目录。

你可能想要获得你选择的目录,你可以通过调用

获得
choicer.getSelectedFile()