简体   繁体   English

如何在不对目录进行硬编码的情况下将某些内容保存到桌面?

[英]How to save something to the desktop without hard-coding the directory?

I was wondering how to get java to save a text file named hello.txt to the desktop without writing 我想知道如何让java在没有写入的情况下将名为hello.txt的文本文件保存到桌面

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

Any help would be great. 任何帮助都会很棒。 so like: 所以喜欢:

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

..and the fileName.getText() is just going to be the 'hello'. ..而fileName.getText()就是'你好'。

UPDATE: i think that i would be able to use the jfilechooser, so would this work? 更新:我认为我将能够使用jfilechooser,所以这会工作吗?

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);
    }
}

I forgot different Locales. 我忘了不同的Locales。 Very fragile code (even for code that starts out OS specific). 非常脆弱的代码 (即使是针对特定操作系统的代码)。 See my comment below re. 请参阅下面的评论。 OS X/ JFileChooser . OS X / JFileChooser

..how the (System.getProperty("user.home"), "Desktop") works.. ..how (System.getProperty("user.home"), "Desktop")工作..

Oracle helpfully provides docs for this kind of thing. Oracle为这类事情提供了有用的文档。

See System.getProperty(String) & new File(String,String) . 请参见System.getProperty(String)new File(String,String)


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). 我将放弃对此的专家(或用户),但我不认为OS X直接在..start屏幕上支持任何应用程序图标或文档图标,默认外观,等等。可能更好地提供结束用户指向user.homeJFileChooser并要求他们将文档保存到桌面(或他们想要的任何地方)。

这会指向桌面目录:

javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()

User.home工作,但只是硬编码目录应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java 8中,如何在不在我的环境中对其进行硬编码的情况下获取主机名? - In Java 8, how do I get my hostname without hard-coding it in my environment? 如何在不对HTML图像进行硬编码的情况下实现Java Servlet图像的幻灯片显示? - How do I implement a slideshow of images from java servlets without hard-coding the images in my html? 如何在不对URL进行硬编码的情况下调用REST API get方法? - How can I call my REST API get method without hard-coding a URL? 从工厂请求项目,而无需对每种情况进行硬编码 - Requesting item from Factory without hard-coding every case 避免硬编码休眠属性 - Avoid hard-coding Hibernate properties 用Java硬编码Map的替代方法? - Alternative ways for hard-coding Map in Java? 从 android 应用程序访问 AWS S3,无需硬编码访问密钥 - Accessing AWS S3 from android app without hard-coding access keys 我可以在没有硬编码的情况下在 Windows 上运行 Java 中构建 Linux 路径吗? - Can I build a Linux path in Java running on Windows without hard-coding? 硬编码与文件输入的效率 - The Efficiency of Hard-Coding vs. File Input 以尺寸单位硬编码按钮的宽度 - Hard-coding button's width in dimension units
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM