前端自动更新copy文件异常问题
问题:在前端应用程序,凌晨自动更新时copy文件,报了一个异常 java.nio.file.FileSystemException: xxxx.jar:另一个程序正在使用此文件,进程无法访问
问题代码:
Files.copy(tempFile.toPath(), oldSrc.toPath(), StandardCopyOption.REPLACE_EXISTING);
异常:
java.nio.file.FileSystemException: xxx.jar: 另一个程序正在使用此文件,进程无法访问
分析下来,这段代码Files.copy 已经运行了一年多了没啥问题,怎么突然就不行了呢?原因是最近新增了守护服务的监控程序bat文件,隔一段时间检测应用程序是否运行正常,自动启动防止掉线。所以是这个守护服务锁定了这个xx.jar文件导致无法copy过去。
问题处理:
FileOutputStream targerFileOutputStream = new FileOutputStream(oldSrc);
FileInputStream sourceFileOutputStream = new FileInputStream(tempFile);
FileChannel targerChannel = targerFileOutputStream.getChannel();
FileChannel sourceChannel = sourceFileOutputStream.getChannel();
targerChannel.transferFrom(sourceChannel,0,sourceChannel.size());
总结:自己玩死了自己。。。