简体   繁体   English

使用Java时在Windows上使用'〜'获取短路径

[英]Get short path with '~' on windows when using java

I have created a java application which downloads files from an online source to my local computer which runs windows 7 我创建了一个Java应用程序,该程序将从在线源下载文件到运行Windows 7的本地计算机。

The code downloads the file but also creates a path for that file so it can be stored in that path 该代码下载了文件,还为该文件创建了一个路径,以便可以将其存储在该路径中。

The file then gets converted to another format 然后将文件转换为另一种格式

The issue that I am having is that it seems that windows don't like it if I navigate to the path using an absolute long path 我遇到的问题是,如果我使用绝对长路径导航到该路径,则似乎Windows不喜欢它

I am using cmd to navigate to the file which means I am creating processes to do that 我正在使用cmd导航到该文件,这意味着我正在创建执行该操作的进程

My code looks like this 我的代码看起来像这样

 String[] command =
        {
                "cmd",
        };
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
            new Thread(new SyncPipe(p.getInputStream(), System.out)).start();

            PrintWriter stdOut = new PrintWriter(p.getOutputStream(), true);
            stdOut.println("cd "+strPath);

As you can see above, the last line is just navigating to the path it works for most cases but not when the path is long and has '-' and other characters. 如上所示,最后一行只是导航到大多数情况下可以使用的路径,但当路径较长且具有'-'和其他字符时,则不会。

If I am on windows explorer and I click on the path at the navigation bar the path shows but with '~' if I embed the '~' path within this code by setting strPath to the path with the '~' everything works fine 如果我在Windows资源管理器上,并且单击导航栏上的路径,则显示该路径,但是如果将“〜”路径嵌入到此代码中(通过将strPath设置为带有“〜”的路径),则显示为“〜”,一切正常

Also if I take the absolute full path from strPath (by printing it to the screen for example) and I open cmd.exe then run the following command 另外,如果我从strPath中获取绝对完整路径(例如通过将其打印到屏幕上)并打开cmd.exe,则运行以下命令

cd <some absolute full path>   <- this works but doesn't work from java 

So I am not sure what I need to do to fix it 所以我不确定该怎么做才能修复它

You can't (and you don't) use ~ for the home folder (that is a shell expansion). 您不能(也不能)将~用作主文件夹(这是Shell扩展)。 You can use System.getProperty("user.home") - which will return the home folder. 您可以使用System.getProperty("user.home") -这将返回主文件夹。

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

相关问题 如何使用 Java 在 Windows 中获取短文件名? - How to get short-filenames in Windows using Java? 有没有办法从java中的完整路径获得“短路径”? - Is there a way to get a “short path” from a full path in java? 使用Java获取阀门在Windows上的应用程序的安装路径? - Get installated path for an app in valve's steam on windows using java? 使用Java在Windows资源管理器中获取选定的文件路径 - get selected file path in windows explorer with Java 获取Windows Server中程序的Java / JVM路径 - get Java/JVM path of the program in Windows Server 获取 Java 中 windows 位置的规范化路径 - Get normalized path for windows location in Java Windows上文件路径的简称未正确返回 - Short name of the file path on Windows is not correctly returned 在Java中使用字节和短数据类型时的处理器使用率 - Processor usage when using byte and short data types in Java 当使用Java锁定窗口时,是否可以获取窗口的屏幕截图? - Is there a way to get screenshot of the windows when it is locked using java? 可以摆脱在Windows 8.1中安装Java 8u60时创建的文件夹的路径吗? - is it ok to get rid of the path of the folder that is created when installing java 8u60 in windows 8.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM