zoukankan      html  css  js  c++  java
  • java的nio之:java的nio系列教程之FileChannel

    一:Java NIO的FileChannel
    ===>Java NIO中的FileChannel是一个连接到文件的通道。可以通过文件通道读写文件。

    ===>FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下。




    二:Java NIO的FileChannel的打开

    ===>在使用FileChannel之前,必须先打开它。但是,我们无法直接打开一个FileChannel,需要通过使用一个InputStream、 OutputStream或RandomAccessFile来获取一个FileChannel实例。

    下面是通过RandomAccessFile打开 FileChannel的示例:

    1    RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
    2    FileChannel inChannel = aFile.getChannel();


    三:Java NIO的FileChannel之从FileChannel读取数据

    调用多个read()方法之一从FileChannel中读取数据。如:

    1    ByteBuffer buf = ByteBuffer.allocate(48);
    2    int bytesRead = inChannel.read(buf);
    首先,分配一个Buffer。从FileChannel中读取的数据将被读到Buffer中。

    然后,调用FileChannel.read()方法。该方法将数据从FileChannel读取到Buffer中。read()方法返回的int值表示了有多少字节被读到了Buffer中。如果返回-1,表示到了文件末尾。


    四:Java NIO的FileChannel之向FileChannel写入数据


    使用FileChannel.write()方法向FileChannel写数据,该方法的参数是一个Buffer。如:

    01    String newData = "New String to write to file..." + System.currentTimeMillis();
    02    
    03    ByteBuffer buf = ByteBuffer.allocate(48);
    04    buf.clear();
    05    buf.put(newData.getBytes());
    06    
    07    buf.flip();
    08    
    09    while(buf.hasRemaining()) {
    10        channel.write(buf);
    11    }
    注意FileChannel.write()是在while循环中调用的。因为无法保证write()方法一次能向FileChannel写入多少字节,因此需要重复调用write()方法,直到Buffer中已经没有尚未写入通道的字节。



    五:Java NIO的FileChannel之关闭FileChannel

    用完FileChannel后必须将其关闭。如:

    1    channel.close();
    FileChannel的position方法

    有时可能需要在FileChannel的某个特定位置进行数据的读/写操作。可以通过调用position()方法获取FileChannel的当前位置。

    也可以通过调用position(long pos)方法设置FileChannel的当前位置。

    这里有两个例子:

    1    long pos = channel.position();
    2    channel.position(pos +123);
    如果将位置设置在文件结束符之后,然后试图从文件通道中读取数据,读方法将返回-1 —— 文件结束标志。

    如果将位置设置在文件结束符之后,然后向通道中写数据,文件将撑大到当前位置并写入数据。这可能导致“文件空洞”,磁盘上物理文件中写入的数据间有空隙。





    六:Java NIO的FileChannel之FileChannel的size方法

    FileChannel实例的size()方法将返回该实例所关联文件的大小。如:

    1    long fileSize = channel.size();




    七:Java NIO的FileChannel之FileChannel的truncate方法

    可以使用FileChannel.truncate()方法截取一个文件。截取文件时,文件将中指定长度后面的部分将被删除。如:

    1    channel.truncate(1024);
    这个例子截取文件的前1024个字节。





    八:Java NIO的FileChannel之FileChannel的force方法

    FileChannel.force()方法将通道里尚未写入磁盘的数据强制写到磁盘上。出于性能方面的考虑,操作系统会将数据缓存在内存中,所以无法保证写入到FileChannel里的数据一定会即时写到磁盘上。要保证这一点,需要调用force()方法。

    force()方法有一个boolean类型的参数,指明是否同时将文件元数据(权限信息等)写到磁盘上。

    下面的例子同时将文件数据和元数据强制写到磁盘上:

    1    channel.force(true);





  • 相关阅读:
    Linux之文件处理命令
    Linux基础命令
    rip实验
    Linux基础之磁盘分区
    mysql安装
    centos Apache、php、mysql默认安装路径
    You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
    Wrong permissions on configuration file, should not be world writable!
    机器会学习么 学习总结
    实验 5 Spark SQL 编程初级实践
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/5823294.html
Copyright © 2011-2022 走看看