桌面路径我认为这将在英语语言的Windows安装只工作:如何获得在Java
System.getProperty("user.home") + "/Desktop";
我怎样才能使这项工作对非英文Windows?
桌面路径我认为这将在英语语言的Windows安装只工作:如何获得在Java
System.getProperty("user.home") + "/Desktop";
我怎样才能使这项工作对非英文Windows?
似乎并不那么容易......
但你可以尝试找到一个anwser浏览一些开源项目,例如的代码在Koders。我想所有的解决方案归结为检查Windows注册表中的HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop
路径。并且可能是Windows特有的。
如果您需要更通用的解决方案,我会尝试找到一个开源应用程序,您知道该应用程序可以在不同平台上正常工作,并在用户的桌面上放置一些图标。
我觉得这是同样的问题...但我不知道!:
In java under Windows, how do I find a redirected Desktop folder?
读它,我希望这个解决方案返回的user.home,但显然不是,答案中的链接评论回来了。还没有尝试过自己。
我想通过使用JFileChooser
解决方案将需要一个非无头JVM,但你可能正在运行其中之一。
这仅适用于Windows。启动REG.EXE并捕获它的输出:
import java.io.*;
public class WindowsUtils {
private static final String REGQUERY_UTIL = "reg query ";
private static final String REGSTR_TOKEN = "REG_SZ";
private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL
+ "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
+ "Explorer\\Shell Folders\" /v DESKTOP";
private WindowsUtils() {}
public static String getCurrentUserDesktopPath() {
try {
Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1) return null;
return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
return null;
}
}
/**
* TEST
*/
public static void main(String[] args) {
System.out.println("Desktop directory : "
+ getCurrentUserDesktopPath());
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;
StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { ; }
}
String getResult() {
return sw.toString();
}
}
}
,或者您可以使用JNA(complete example here)
Shell32.INSTANCE.SHGetFolderPath(null,
ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT,
pszPath);
使用JNA比调用一个进程并解析结果要好得多。 – Sebastian 2014-06-15 15:36:07
public class Sample {
public static void main(String[] args) {
String desktopPath =System.getProperty("user.home") + "\\"+"Desktop";
String s = "\"" + desktopPath.replace("\\","\\\\") + "\\\\" +"satis" + "\"";
System.out.print(s);
File f = new File(s);
boolean mkdir = f.mkdir();
System.out.println(mkdir);
}
}
这只会返回英文版本,这不是OP所要的。 – asteri 2012-10-29 11:47:20
不要这样做。如果用户移动了桌面,这将不起作用!如果您有固态C:驱动器,则将桌面移动到其他驱动器上很常见。这会阻止大量写入SSD(读操作不会缩短生命周期,写操作),这意味着您可以拥有一个较小的C:驱动器和一个很大的正常驱动器。 – 2015-06-28 22:36:59
我使用Windows的指令,并与它法文版:
System.getProperty("user.home") + "/Desktop";
适合我。
不要这样做。如果用户移动了桌面,这将不起作用!如果您有固态C:驱动器,则将桌面移动到其他驱动器上很常见。这会阻止大量写入SSD(读操作不会缩短生命周期,写操作),这意味着您可以拥有一个较小的C:驱动器和一个很大的正常驱动器。 – 2015-06-28 22:36:15
最简单的解决方案是找出机器名称,因为这个名称只是变量在Desktop文件夹路径中的变量。所以如果你能找到这个,你已经找到了桌面的路径。下面的代码应该做的伎俩 - 它为我做:)
String machine_name = InetAddress.getLocalHost().getHostName();
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/";
有2件事情。
\
而不是/
。\\
)来分隔文件夹名称。很多java类接受/作为分隔符。这是完全有效的:File f = new File(“S:/folder/file.txt”); – 2015-06-28 22:39:06
您的斜线不正确。你应该像这样使用它。
try{
String sys = System.getProperty("user.home");
String fileurl = sys+ "\\Desktop\\new";
File newfile = new File(fileurl);
newfile.mkdir();
}catch(Exception ioe){
// Handle the error
System.out.println("Error");
}
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()
上面提到的链接给出了正确的答案。 `File home = FileSystemView.getFileSystemView()。getHomeDirectory();`然后如果你需要它作为一个字符串`家。getAbsolutePath();` – 2015-06-28 22:53:48