java返回false for文件#的存在

发布于 2025-02-05 07:02:35 字数 421 浏览 3 评论 0原文

新文件(“ c:/users/jredfox/appdata/local/microsoft/windowsapps/windowsapps/wt.exe”)。 。关于如何使其工作的想法?

也尝试了

        File f = new File("C:/Users/jredfox/AppData/Local/Microsoft/WindowsApps");
        for(File a : f.listFiles())
        {
            System.out.println(a + " isSymbolicLink:" + Files.isSymbolicLink(a.toPath()) + " exists:" + Files.exists(a.toPath()));
        }

new File("C:/Users/jredfox/AppData/Local/Microsoft/WindowsApps/wt.exe").exists() returns false when the exact path copied and pasted to command prompt starts windows terminal. Any ideas of how to get it working?

also tried

        File f = new File("C:/Users/jredfox/AppData/Local/Microsoft/WindowsApps");
        for(File a : f.listFiles())
        {
            System.out.println(a + " isSymbolicLink:" + Files.isSymbolicLink(a.toPath()) + " exists:" + Files.exists(a.toPath()));
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2025-02-12 07:02:35

似乎找不到答案,但我找到了解决方法。

    private static boolean isWindows = osName.contains("windows");
    private static boolean isLinux = osName.contains("linux") || osName.contains("nux") || osName.contains("aix");
    private static boolean isMac = osName.contains("mac") && !isLinux || osName.contains("osx") || osName.contains("darwin");
    /**
     * find executeable from path. support for WUP and macOs apps as well as standard with and without extensions paths
     */
    public static String findExe(String name)
    {
        String ext = isWindows() ? ".exe" : isMac() ? ".app" : "";//TODO: test macOs and confirm functionality on windows
        String fname = name.contains(".") ? name : name + ext;
        boolean hasF = !ext.isEmpty();
        
        //search the full path of the dir before searching env path
        if(name.contains("/"))
        {
            File path = new File(name);
            File fpath = new File(fname);
            if(path.canExecute())
                return path.getPath();
            else if(hasF && isExe(fpath))
                return fpath.getPath();
        }

        for (String dirname : System.getenv("PATH").split(File.pathSeparator)) 
        {
            File file = new File(dirname, name);
            File ffile = new File(dirname, fname);
            
            //Windows 10 WUP support
            if(OSUtil.isWindows())
            {
                if(dirname.contains("WindowsApps"))
                {
                    File[] files = file.getParentFile().listFiles();
                    if(FileUtil.containsFile(files, file) && !file.isDirectory())
                        return file.getPath();
                    else if(FileUtil.containsFile(files, ffile) && !file.isDirectory())
                        return ffile.getPath();
                }
            }
            if (isExe(file))
                return file.getPath();
            else if(hasF && isExe(ffile))
                return ffile.getPath();
        }
        
        //macOS start here
        if(OSUtil.isMac())
        {
            for(String root : macAppPaths)
            {
                File app = new File(root, fname);
                if(isExe(app))
                    return app.getPath();
            }
        }
        
        return null;
    }
        public static boolean isWindows()
    {
        return isWindows;
    }
    
    public static boolean isMac()
    {
        return isMac;
    }
    
    public static boolean isLinux()
    {
        return isLinux;
    }
    public static boolean isExe(File f)
    {
        return (f.isFile() || f.getName().endsWith(".app")) && f.canExecute();
    }

couldn't seem to find an answer but I found a workaround.

    private static boolean isWindows = osName.contains("windows");
    private static boolean isLinux = osName.contains("linux") || osName.contains("nux") || osName.contains("aix");
    private static boolean isMac = osName.contains("mac") && !isLinux || osName.contains("osx") || osName.contains("darwin");
    /**
     * find executeable from path. support for WUP and macOs apps as well as standard with and without extensions paths
     */
    public static String findExe(String name)
    {
        String ext = isWindows() ? ".exe" : isMac() ? ".app" : "";//TODO: test macOs and confirm functionality on windows
        String fname = name.contains(".") ? name : name + ext;
        boolean hasF = !ext.isEmpty();
        
        //search the full path of the dir before searching env path
        if(name.contains("/"))
        {
            File path = new File(name);
            File fpath = new File(fname);
            if(path.canExecute())
                return path.getPath();
            else if(hasF && isExe(fpath))
                return fpath.getPath();
        }

        for (String dirname : System.getenv("PATH").split(File.pathSeparator)) 
        {
            File file = new File(dirname, name);
            File ffile = new File(dirname, fname);
            
            //Windows 10 WUP support
            if(OSUtil.isWindows())
            {
                if(dirname.contains("WindowsApps"))
                {
                    File[] files = file.getParentFile().listFiles();
                    if(FileUtil.containsFile(files, file) && !file.isDirectory())
                        return file.getPath();
                    else if(FileUtil.containsFile(files, ffile) && !file.isDirectory())
                        return ffile.getPath();
                }
            }
            if (isExe(file))
                return file.getPath();
            else if(hasF && isExe(ffile))
                return ffile.getPath();
        }
        
        //macOS start here
        if(OSUtil.isMac())
        {
            for(String root : macAppPaths)
            {
                File app = new File(root, fname);
                if(isExe(app))
                    return app.getPath();
            }
        }
        
        return null;
    }
        public static boolean isWindows()
    {
        return isWindows;
    }
    
    public static boolean isMac()
    {
        return isMac;
    }
    
    public static boolean isLinux()
    {
        return isLinux;
    }
    public static boolean isExe(File f)
    {
        return (f.isFile() || f.getName().endsWith(".app")) && f.canExecute();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文