尽管在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();
}
答案 0 :(得分:0)
我使用两个线程@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale( permission );
if (! showRationale) {
// user also CHECKED "never ask again"
}
}
}
}
}
解决了这个问题。
一个正在生成要写入文件的数据的线程正在将这些数据对象放入Producer and Consumer
,另一个线程正在使用LinkedBlockingQueue
()方法从该队列中获取数据并将数据对象持久化到文件中。 / p>
使用take
优于其他方法的优点是,它提供了LinkedBlockingQueue
()方法,该方法检索并删除此take
的{{1}},如有必要,直到某个元素可用为止和head
方法,直到有足够的可用空间为止。