gpt4 book ai didi

java - 如何使用java在程序文件中移动文件?

转载 作者:行者123 更新时间:2023-12-01 06:21:46 25 4
gpt4 key购买 nike

我需要以编程方式在程序文件中移动一些conf 文件。目标环境是windows。这是我的示例代码: https://gist.github.com/gandra/911f465217011e2dfec1a8e561c0bb57但执行后我收到以下错误:

"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=59066:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\target\classes;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\sahi.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\ant-sahi.jar;C:\Users\draganmijatovic\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\draganmijatovic\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\draganmijatovic\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-core\2.12.0\log4j-core-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-api\2.12.0\log4j-api-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar" MoveFilesDemo
Exception in thread "main" java.nio.file.AccessDeniedException: C:\temp\testfile.txt -> C:\Program Files\testfile.txt
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at MoveFilesDemo.main(MoveFilesDemo.java:10)

Process finished with exit code 1

有没有办法以管理员身份运行它或任何其他选项来实现它?要设置一些java权限等...请不要建议我将此文件保留在程序数据中或重复我不应该像下面的问题一样绕过它:Bypass Windows permission restrictions on program files folder

这是某些旧桌面应用程序的自动化测试的一部分,需要修复此问题才能进行测试。因此,我完全可以接受绕过安全性或任何其他选项,这将帮助我在执行期间无需人工干预的情况下实现这一目标。

最佳答案

您需要像这样更改权限:

// changing the file permissions 
file.setExecutable(true);
file.setReadable(true);
file.setWritable(true);

关于java - 如何使用java在程序文件中移动文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57558429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com