Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java
我想将结果写入用户的桌面,而不是写入与我正在运行的文件类相同的目录。
我使用的是 Mac 操作系统。在 Window 中怎么样?1
谢谢
最佳答案
用户的主目录是:
System.getProperty("user.home")
一般来说 +"/Desktop"就可以,但不可移植。
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
...
} finally {
out.close();
}
这会将文件“mytext.txt”写入主目录。
关于java - 将文件写入桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357022/