java - 进程无法访问文件,因为该文件正在被另一个进程使用

标签 java file nio file-writing

虽然 stackoverflow 上有很多这样的问题,但对我来说,它在快速写入文件时出错,该方法在一分钟内被调用大约 180 次以将数据写入文件。当写入频率非常快时,它会给出错误:

java.nio.file.FileSystemException: C:\dropcopy\DGCX_Report_Files\TradeReport_18072018.csv: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
at java.nio.file.Files.newOutputStream(Files.java:216)
at java.nio.file.Files.write(Files.java:3292)
at com.algo.fix.common.TradeFileManager.appendInFile(TradeFileManager.java:542)
at com.algo.fix.common.TradeFileManager.persistExecutions(TradeFileManager.java:469)
at com.algo.fix.common.TradeFileManager.processTradeCaptureReport(TradeFileManager.java:768)
at com.algo.fix.common.BaseApplication.onMessage(BaseApplication.java:129)
at quickfix.fix50sp2.MessageCracker.crack11(MessageCracker.java:253)
at quickfix.fix50sp2.MessageCracker.crack(MessageCracker.java:217)
at com.algo.fix.common.BaseApplication.fromApp(BaseApplication.java:97)
at quickfix.Session.fromCallback(Session.java:1818)
at quickfix.Session.verify(Session.java:1762)
at quickfix.Session.verify(Session.java:1833)
at quickfix.Session.next(Session.java:1030)
at quickfix.Session.next(Session.java:1173)
at quickfix.mina.SingleThreadedEventHandlingStrategy$SessionMessageEvent.processMessage(SingleThreadedEventHandlingStrategy.java:151)
at quickfix.mina.SingleThreadedEventHandlingStrategy.block(SingleThreadedEventHandlingStrategy.java:101)
at quickfix.mina.SingleThreadedEventHandlingStrategy.lambda$blockInThread$0(SingleThreadedEventHandlingStrategy.java:133)
at quickfix.mina.SingleThreadedEventHandlingStrategy$ThreadAdapter$RunnableWrapper.run(SingleThreadedEventHandlingStrategy.java:244)
at java.lang.Thread.run(Thread.java:745)

但是在这个错误之后,它再次恢复写入。跳过一些数据到文件。 我写入文件的代码是:

try{
        String data = sb.toString();
        byte[] dataByte= data.getBytes();
        Files.write(file, dataByte, StandardOpenOption.APPEND);                              
        Files.write(file, System.getProperty("line.separator").getBytes(), StandardOpenOption.APPEND);
        log.info("Wrote to file.");
    } catch(Exception e ){
        log.info("Not able to write to file.");
        e.printStackTrace();
    }

最佳答案

我使用两个线程生产者和消费者解决了这个问题。 一个生成要写入文件的数据的线程将这些数据对象放入 LinkedBlockingQueue 中,另一个线程使用 take() 方法从该队列中获取数据并将数据对象保存到文件中。

使用 LinkedBlockingQueue 相对于其他队列的优势在于,它提供了 take() 方法,该方法检索并删除此queuehead(如有必要,等待直到元素变得可用)和 put() 方法(该方法等待必要的空间可用)。

关于java - 进程无法访问文件,因为该文件正在被另一个进程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51415551/

相关文章:

vb.net - 删除名称包含字符串(匹配掩码)的 FTP 文件

java - RandomAccessFile 与 FileChannel.open(path);

java - Java中如何迭代读取目录中的所有文件?

java - 仅打印最后添加的项目

java - 自定义添加应用程序链接不起作用

java - 创建对象枚举

linux - 在 bash 中将一个文件 append 到另一个文件的中间

Java:循环列表两次会消耗双倍功率吗?

c++ - Atmel Studio 中的文件

java - 从选择器中移除按键