[英]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.home
的JFileChooser
并要求他们将文档保存到桌面(或他们想要的任何地方)。
这会指向桌面目录:
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()
User.home工作,但只是硬编码目录应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.