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

Why does JFileChooser work this way?

大家好,好久不见了。所以我正在开发这个应用程序,它允许我制作和保存文档等。这不是这个问题的重点。所以在保存功能中,我决定使用 JFileChooser 来允许用户 select 保存文件的位置。

现在我的 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 名称。这个我以后会重点说。

现在我发现了两个问题。首先,我必须去一个“深”文件夹来保存我的文件。 解释:
假设我想将我的文件保存在用户的 Public 中,我的目录如下所示:

C:\Users\Public
现在我的代码在文件参数中添加了 \hi.txt ,这不应该保存在 Public 文件夹中吗?如果我在打开 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(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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.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.run(Unknown Source)
at java.awt.EventQueue.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)

如果我在 public 中打开一个文件夹然后单击保存,我的目录现在看起来像:

C:\Users\Public\下载

然后它不会保存在“下载”文件夹中,而是保存在 Public 用户文件夹中。

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

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

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

choicer.getSelectedFile()