如何在不对目录进行硬编码的情况下将某些内容保存到桌面? [英] How to save something to the desktop without hard-coding the directory?

查看:146
本文介绍了如何在不对目录进行硬编码的情况下将某些内容保存到桌面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何让java将名为 hello.txt 的文本文件保存到桌面而无需编写

I was wondering how to get java to save a text file named hello.txt to the desktop without writing

"C:\\Users\\Austin\\Desktop"

任何帮助都会很棒。所以喜欢:

Any help would be great. so like:

FileWriter fileWriter = new FileWriter(fileName.getText(), true);

..和 fileName.getText()将会是'你好'。

..and the fileName.getText() is just going to be the 'hello'.

更新:
i认为我可以使用jfilechooser,这会有用吗?

UPDATE: i think that i would be able to use the jfilechooser, so would this work?

JFileChooser chooser = new JFileChooser();
chooser.setVisible(true);

会起作用吗?如果是这样,我如何使用那里的选择来保存文件?
即时通讯.... :(

would that work? and if so, how would i get it to save the file using the selection in there? im a noob.... :(

推荐答案

import java.io.File;

class FindDesktopOnWindows {

    public static void main(String[] args) throws Exception {
        if (System.getProperty("os.name").toLowerCase().indexOf("win")<0) {
            System.err.println("Sorry, Windows only!");
            System.exit(1);
        }
        File desktopDir = new File(System.getProperty("user.home"), "Desktop");
        System.out.println(desktopDir.getPath() + " " + desktopDir.exists());

        java.awt.Desktop.getDesktop().open(desktopDir);
    }
}

我忘记了不同的Locales。非常脆弱的代码(即使是针对特定操作系统的代码)。请参阅下面的评论re.OS X / JFileChooser

I forgot different Locales. Very fragile code (even for code that starts out OS specific). See my comment below re. OS X/JFileChooser.


.. 如何( System.getProperty(user.home),桌面)工作..

Oracle为这类事情提供了有用的文档。

Oracle helpfully provides docs for this kind of thing.

参见 System.getProperty(String) &
新文件(字符串,字符串)

我会放弃专家(或用户),但我不认为OS X直接在..start屏幕上支持任何应用程序图标或文档图标,默认外观,无论..可能最好为最终用户提供 JFileChooser 指向 user.home 并要求他们保存文档到桌面(或他们想要的任何地方)。

I'll cede to an expert (or a user) on this, but I don't think OS X supports any application icons or document icons directly on the ..start screen, default look, whatever.. Probably better to offer the end user a JFileChooser pointing to user.home and ask them to save the document to the desktop (or wherever they feel like).

这篇关于如何在不对目录进行硬编码的情况下将某些内容保存到桌面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆