zoukankan      html  css  js  c++  java
  • Java NIO read/write file through FileChannel

    referee:  Java NIO FileChannel

    A java nio FileChannel is an channel that is connected to a file. Using a file channel you can read data from a file, and write data to a file. NIO is an alternative to reading files with the standard Java IO API.

    A FileChannel cannot be set into non-blocking mode, and it's always runs in blocks mode.

    Opening a FileChannel

    To use an FileChannel, we should firstly open it. And we should obtain a FileChannel via in InputStream, OutputStream, or an RandomAccessFile. Here is how you can open a FileChannel via RandomChannel:

     RandomAccessFile aFile = new RandomAccessFile("./testNewFile.txt", "rw");
     FileChannel inChannel =  aFile.getChannel();
    View Code

    Reading Data from FileChannel

    we use  read() methods to read  data from FileChannel. Here is an example:

            ByteBuffer buf = ByteBuffer.allocate(48);
            int bytesRead = inChannel.read(buf);
    View Code

    Firstly we allocated a buffer, and read data from channel into buffer. And the method FileChannel.read() return an integer tells how many bytes written into the Buffer. If -1 is returned, then it means end-of-file is reached.

    Write Data into a FileChannel

    We call method FileChannel.write() to write data into the Buffer. Here is an example:

            String newData = "New String to write file..." + System.currentTimeMillis();
            ByteBuffer newBuf = ByteBuffer.allocate(48);
            newBuf.clear();
            newBuf.put(newData.getBytes());
            String v = new String(newBuf.array());
            System.out.println("content of new byte buffer: " + v);
    View Code

    FileChannel Position

    When reading or writing to a FileChannel at an position, we can get the current of the position of the FileChannel object by calling the position() method.

    We can set the position value by calling method position(long pos), and here are the examples:

            long pos = fileChannel.position();
            System.out.println(pos);
            fileChannel.position(pos - 10);
            System.out.println(fileChannel.position());
    View Code

    Note:

    The the position value we set bigger than length of channel, and then try to read data from channel,  then we will get minus 1 showing end-of-file.

    And if we try to write data to an position bigger than the length of channel, the file will be extended to fit the position and the writing method can run successfully. But this action will cause an "file hole" meaning file on the disk has graps.

    FileChannel Size

    We can call method size() to get the length of channel

    FileChannel Truncate

    We can truncate a file by calling the FileChannel.truncate() method. When you truncate a file, we cut it off at a given length. 

    FileChannel Force

    The force method will flush all unwritten data from the channel to the disk. An operating system may cache data in memory for high performance, so we should take flush to keep data consisentency.

            String content = new String("Before flush: " + Files.readAllBytes(Paths.get("testNewFile.txt")));
            System.out.println(content);
    
            inChannel.force(true);
    
            String contentAfterFlush = new String("After flush: " + Files.readAllBytes(Paths.get("testNewFile.txt")));
            System.out.println(contentAfterFlush);
    View Code

    程序源代码:

            String newData = "New String to write file..." + System.currentTimeMillis();
            ByteBuffer newBuf = ByteBuffer.allocate(48);
            newBuf.clear();
            newBuf.put(newData.getBytes());
            String v = new String(newBuf.array());
            System.out.println("content of new byte buffer: " + v);
    
            newBuf.flip();
            while(newBuf.hasRemaining()){
                inChannel.write(newBuf);
            }
            long pos = inChannel.position();
            System.out.println(pos);
            inChannel.position(pos - 10);
            System.out.println("position of channel: " + inChannel.position());
    
            // size of the file channel
            System.out.println("size of channel: "+inChannel.size());
    
            String content = new String(Files.readAllBytes(Paths.get("testNewFile.txt")));
            System.out.println(content);
    
            inChannel.force(true);
    
            String contentAfterFlush = new String(Files.readAllBytes(Paths.get("testNewFile.txt")));
            System.out.println(contentAfterFlush);
    
    
            //close file channel
            inChannel.close();
    View Code
  • 相关阅读:
    结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
    深入理解系统调用
    基于mykernel2.0编写一个操作系统内核
    如何评测一个软件工程师的计算机网络知识水平与网络编程技能水平?
    如何评测软件工程知识技能水平?
    深入理解TCP协议及其源代码
    Socket与系统调用深度分析
    创新产品的需求分析:未来的图书会是什么样子?
    构建调试Linux内核网络代码的环境MenuOS系统
    解决npm ERR! code ELIFECYCLE npm ERR! errno 1问题
  • 原文地址:https://www.cnblogs.com/wenwangt/p/5118556.html
Copyright © 2011-2022 走看看