日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java获取被占用的文件进程_java – 进程无法访问该文件,因为它正被另一个进程使用...

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java获取被占用的文件进程_java – 进程无法访问该文件,因为它正被另一个进程使用... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有一段代碼監視目錄以添加文件.每當將新文件添加到目錄時,將挑選該文件的內容并在kafka上發布,然后刪除該文件.

這在我發出單個請求時有效,但是一旦我將代碼從jMeter請求5或10個用戶請求,內容就會在kafka上成功發布,但代碼無法刪除該文件.我收到一個FileSystemException,其中包含一個消息,該進程無法訪問該文件,因為它正由另一個進程使用.

我想有一些我無法看到的并發問題.

public void monitor() throws IOException, InterruptedException {

Path faxFolder = Paths.get(TEMP_FILE_LOCATION);

WatchService watchService = FileSystems.getDefault().newWatchService();

faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

boolean valid = true;

do {

WatchKey watchKey = watchService.take();

for (WatchEvent> event : watchKey.pollEvents()) {

if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {

String fileName = event.context().toString();

publishToKafka(new File(TEMP_FILE_LOCATION + fileName).toPath(), "topic");

}

}

valid = watchKey.reset();

} while (valid);

}

private void publishToKafka(Path path, String topic) {

try (BufferedReader reader = Files.newBufferedReader(path)) {

String input = null;

while ((input = reader.readLine()) != null) {

kafkaProducer.publishMessageOnTopic(input, topic);

}

} catch (IOException e) {

LOG.error("Could not read buffered file to send message on kafka.", e);

} finally {

try {

Files.deleteIfExists(path); // This is where I get the exception

} catch (IOException e) {

LOG.error("Problem in deleting the buffered file {}.", path.getFileName(), e);

}

}

}

異常日志:

java.nio.file.FileSystemException: D:\upload\notif-1479974962595.csv: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)

at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)

at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)

at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)

at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)

at java.nio.file.Files.deleteIfExists(Unknown Source)

at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.publishToKafka(MonitorDirectory.java:193)

at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.sendData(MonitorDirectory.java:125)

at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.run(MonitorDirectory.java:113)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

總結

以上是生活随笔為你收集整理的java获取被占用的文件进程_java – 进程无法访问该文件,因为它正被另一个进程使用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。