【问题标题】:How to select a path to save a file "near" user.dir如何选择保存文件“in”user.dir 的路径
【发布时间】:2015-02-26 11:47:17
【问题描述】:

我想这样做:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir" + "\\datos_medidas")));

但我收到 NullPointerException。

我要做的就是将当前目录路径放在 user.dir 文件夹中的文件夹中,我在 user.dir 中有一个文件夹,我想将我的文件保存在该文件夹中,但我没有不知道怎么做。

我不能使用“文字路径”我需要一个相对路径,因为这个应用程序将在所有 Windows 版本上运行,我不能使用文字路径。

【问题讨论】:

    标签: java file path save jfilechooser


    【解决方案1】:

    我认为原因很明显。检查您的代码:

    System.getProperty("user.dir" + "\\datos_medidas")
    

    您尝试检索不存在的系统属性。相反,您应该检索代表文件系统路径的系统属性 user.dir 并创建使用此路径作为父路径的 File 对象:

    new File(System.getProperty("user.dir"), "datos_medidas"))
    

    【讨论】:

    • 你能告诉我如何在 setCurrentDirectory 上使用“MyDocuments”吗?
    • 好像没看懂你的问题。 “datos_medias”和“我的文档”有什么区别?
    • 不同之处在于“datos_medias”是我创建的一个文件夹,因此它始终具有相同的名称和相同的路径,但“我的文档”文件夹在英文 windows vista 上可能是“文档”但在西班牙语中,它被称为“Mis Documentos”,我问你是否知道如何设置 currentDirectory 路径始终在“我的文档”窗口文件夹中工作,它是由操作系统创建的,但在其他语言中被称为不同。我认为我不能使用 ("user.home", "Documents"),因为它在西班牙语版本中不起作用,不是吗?
    • 这是完全不同的问题。我有答案但我不想在这里写垃圾,所以,问这个问题,我会回复你。
    • 我需要 90 分钟来发布其他问题...所以我必须等待...(或者您可以在此处通过电子邮件发布您的答案...您想要什么)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多