简体   繁体   English

使用Java Nio进行写操作期间如何检测磁盘已满?

[英]How to detect disk full during write operation with Java nio?

I would like to write a file that comes from over a network, so I don't know the size of the file that's comming in. Sometimes the disk on the file server might get filled up and I would like return a message to my client notifying them of this error. 我想写一个来自网络的文件,所以我不知道要传入的文件的大小。有时文件服务器上的磁盘可能已满,我想向客户端返回一条消息通知他们这个错误。 I couldn't find any documentation on being able to catch this type of i/o error. 我找不到任何有关能够捕获此类I / O错误的文档。 FileChannel streams bytes from memory to disk, so it may not be trivial to detect this. FileChannel将字节从内存流传输到磁盘,因此检测到它可能并不容易。 Is the saving happening asynchronously? 节省是异步发生的吗? Is it possible to detect disk full? 是否可以检测到磁盘已满?

// Create a new file to write to
RandomAccessFile mFile = new RandomAccessFile(this.mFilePath, "rw");
FileChannel mFileChannel = this.mFile.getChannel();

// wrappedBuffer has my file in it
ByteBuffer wrappedBuffer = ByteBuffer.wrap(fileBuffer);
while(wrappedBuffer.hasRemaining()) {
    bytesWritten += this.mFileChannel.write(wrappedBuffer, this.mBytesProcessed);
}

I figured in the File class, we can do something like this: 我发现在File类中,我们可以执行以下操作:

// if there is less than 1 mb left on disk
new File(this.mFilePath, "r").getUsableSpace() < 1024; 

But if there a way to throw an except if this.mFileChannel.write() fails because the disk is full? 但是如果由于磁盘已满而this.mFileChannel.write()失败,是否有抛出this.mFileChannel.write()的方法?

Even if it's not recommended to parse the error message you could do something like this : 即使不建议解析错误消息,您也可以执行以下操作:

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;

public class SmallDisk {

    final static String SMALL_DISK_PATH = "/Volumes/smallDisk";

    final static Pattern NO_SPACE_LEFT = Pattern.compile(": No space left on device$");

    public static void main(String[] args) throws NoSpaceException {
        Path p = Paths.get(SMALL_DISK_PATH);
        FileStore fs = null;
        try {
            fs = Files.getFileStore(p);
            System.out.println(fs.getUsableSpace());
            Path newFile = Paths.get(SMALL_DISK_PATH + "/newFile");
            Files.createFile(newFile);

        } catch (FileSystemException e) {
            //We catch the "No space left on device" from the FileSystemException and propagate it
            if(NO_SPACE_LEFT.matcher(e.getMessage()).find()){
                throw new NoSpaceException("Not enough space");
            }
            //Propagate exception or deal with it here
        } catch (IOException e) {
            //Propagate exception or deal with it here
        }

    }

    public static class NoSpaceException extends IOException{

        public NoSpaceException(String message) {
            super(message);
        }
    }
}

Another way but it doesn't guaranty that you won't have exception is to use the FileStore to check that you enough space before you write (not enough if you are working with a shared folder or multi threads software) 另一种方法,但不能保证您不会例外,那就是使用FileStore在写入之前检查是否有足够的空间(如果使用共享文件夹或多线程软件,则空间不足)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM