简体   繁体   English

如果Filechannel.force(true)引发异常,则写入的数据将如何处理?

[英]What happens with written data if Filechannel.force(true) throws an exception?

What happens, when calling filechannel.force and throws an exception? 调用filechannel.force并引发异常时会发生什么?

When filechannel.write was successful and we were able to write the whole buffer content into the filechannel i want to be sure if everything was flushed to disk. 当filechannel.write成功并且我们能够将整个缓冲区内容写入filechannel时,我想确定是否所有内容都已刷新到磁盘。 I'll call force(true), which can throw an exception. 我将调用force(true),它会引发异常。 What happens with data i wrote into filechannel? 我写入文件通道的数据会怎样? Was nothing flushed to disk or may be some part of data is flushed? 是否没有东西刷新到磁盘上,或者可能有一部分数据被刷新了? How can i rollback the operation? 如何回滚该操作?

public boolean flush(ByteBuffer[] buffers, long buffersRemaining)
{
    try
    {
        FileChannel fileChannel = new RandomAccessFile(target, "rw").getChannel();
         long writtenBytes = fileChannel.write(buffers);  
         //we think we have written all the data into filechannel
         if(writtenBytes == buffersRemaining)
         {
            //we try to flush, but we get an exception
            try
            {
                fileChannel.force(true);
                fileChannel.close();
            }
            catch(IOException exception)
            {
                //???
                return false;
            }
         }                 
    }
    catch(Exception exception)
    {
      //log exception
      return false;
    }
}

You should only try to handle exceptions if you can do something about them. 如果可以对异常进行某些处理,则应仅尝试处理它们。 This is one of these cases where it does not make sense. 这是其中没有意义的情况之一。 This should not happen normally, just log the exception and fail gracefully. 这通常不应该发生,只需记录异常并优雅地失败即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java FileChannel.force()和StandardOpenOption.Sync之间的区别是什么? - What is the diff between Java FileChannel.force() and StandardOpenOption.Sync? 是否需要FileChannel.force和FileDescriptor.sync? - Are FileChannel.force and FileDescriptor.sync both needed? RandomAccessFile.close()是否在内部调用FileChannel.force()? - Does RandomAccessFile.close() internally call FileChannel.force()? 线程抛出异常时会发生什么? - what happens when a Thread throws an Exception? 如果java flyway迁移抛出异常会发生什么? - What happens if a java flyway migration throws an exception? 如果方法抛出未在方法声明中使用“throws”指定的异常,会发生什么情况 - What happens if a method throws an exception that was not specified in the method declaration with “throws” 在Java中,评估构造函数调用的参数引发异常时会发生什么? - In Java, what happens when evaluating the arguments of a constructor call throws an exception? 线程池执行时抛出异常时线程会发生什么? - What happens to a Thread when it throws an exception when executed by a thread pool? 当数据写入服务器端的 socket.getOutputStream 时会发生什么? - What happens when data is written to socket.getOutputStream on server side? 当自动关闭资源时尝试使用资源抛出异常以及异常时会发生什么 - What happens when try with resources throws an exception along with an exception when closing the resources automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM