【问题标题】:How to get complete file name and path of file which is opened in java program?如何获取在java程序中打开的文件的完整文件名和路径?
【发布时间】:2013-08-20 11:53:41
【问题描述】:

我创建了一个 html 编辑器,我想在 JTextPane 中获取打开文件的文件名和路径。有什么建议吗?

【问题讨论】:

  • 你能发布一些示例代码吗?
  • “我想获取打开文件的文件名和路径” 你(或你的代码)没有告诉JEditorPane要加载什么文件吗?保留对它的引用!
  • file.getPath() 方法解决了我的问题。

标签: java swing html-editor


【解决方案1】:

假设您使用文件选择器(文件选择器),这对于代码编辑器来说似乎很可能,您可以简单地保存您收到的文件路径:

public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //At this point you can use: file.getName() to get your filename
            //You can also use file.getPath()
        } else {
            //Canceled opening
        }
    }
}

您可以将 file.getName() 和 file.getPath() 的结果保存到稍后分配给 JTextPane 的字符串中。

有关文件选择器的更多信息,请参阅documentation,它也更详细地解释了此过程。

如果您使用 File,您可以使用提供相同信息的相同功能。

【讨论】:

  • 非常感谢... file.getName() 和 file.getPath() 对我有用...!
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多