如何获取在java程序中打开的完整文件名和文件路径?

时间:2013-08-20 11:53:41

标签: java swing html-editor

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

1 个答案:

答案 0 :(得分:2)

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

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,您可以使用提供相同信息的相同功能。

相关问题