java 如何从用户那里获取保存文件的路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2160907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:46:05  来源:igfitidea点击:

How to get path to save a file from user?

java

提问by Supereme

I'm developing an application in which I want to create an 'MS-EXCEL' file and for that I want the name for the file to be given by user and later on I want to provide an option to the user to save at the path where it wants. I know about JFileChooser but it only allows to select a file from the existing files whereas I want to save. What can be the way? Please give me some brief description.

我正在开发一个应用程序,我想在其中创建一个“MS-EXCEL”文件,为此我希望用户提供文件的名称,稍后我想为用户提供一个选项以保存在它想要的路径。我知道 JFileChooser 但它只允许从现有文件中选择一个文件,而我想保存。有什么办法?请给我一些简短的描述。

回答by Ash

You can use JFileChooser.setFileSelectionModeto allow selection of either files, directories, or both files and directories.

您可以使用JFileChooser.setFileSelectionMode来允许选择文件、目录或文件和目录。

You can also type in the name of a file that doesn't already exist.

您还可以键入尚不存在的文件的名称。

回答by stacker

A full example on FileChooser you find here

您可以在此处找到有关 FileChooser 的完整示例

回答by willcodejavaforfood

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int option = chooser.showSaveDialog(null);
    if (option == JFileChooser.APPROVE_OPTION)
    {
        // do soemthing
    }

回答by Itay Maman

Have you tried the JFileChooser's showSaveDialog()method?

您是否尝试过 JFileChooser 的showSaveDialog()方法?

回答by Robert

Is there a JDirectoryChooser. Sounds to easy!

是否有 JDirectoryChooser。听起来很容易!