[Java] Java NIO在文件末尾追加数据 →→→→→进入此内容的聊天室

来自 , 2020-10-04, 写在 Java, 查看 198 次.
URL http://www.code666.cn/view/b9d487a3
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. import java.nio.MappedByteBuffer;
  5. import java.nio.channels.FileChannel;
  6.  
  7. public class MappedFile {
  8.        
  9.         // 文件名
  10.         private String fileName;
  11.  
  12.         // 文件所在目录路径
  13.         private String fileDirPath;
  14.  
  15.         // 文件对象
  16.         private File file;
  17.  
  18.         private MappedByteBuffer mappedByteBuffer;
  19.         private FileChannel fileChannel;
  20.         private boolean boundSuccess = false;
  21.  
  22.         // 文件最大只能为50MB
  23.         private final static long MAX_FILE_SIZE = 1024 * 1024 * 50;
  24.        
  25.         // 最大的脏数据量512KB,系统必须触发一次强制刷
  26.         private long MAX_FLUSH_DATA_SIZE = 1024 * 512;
  27.  
  28.         // 最大的刷间隔,系统必须触发一次强制刷
  29.         private long MAX_FLUSH_TIME_GAP = 1000;
  30.  
  31.         // 文件写入位置
  32.         private long writePosition = 0;
  33.  
  34.         // 最后一次刷数据的时候
  35.         private long lastFlushTime;
  36.  
  37.         // 上一次刷的文件位置
  38.         private long lastFlushFilePosition = 0;
  39.        
  40.         public MappedFile(String fileName, String fileDirPath) {
  41.                 super();
  42.                 this.fileName = fileName;
  43.                 this.fileDirPath = fileDirPath;
  44.                 this.file = new File(fileDirPath + "/" + fileName);
  45.                 if (!file.exists()) {
  46.                         try {
  47.                                 file.createNewFile();
  48.                         } catch (IOException e) {
  49.                                 e.printStackTrace();
  50.                         }
  51.                 }
  52.                
  53.         }
  54.  
  55.         /**
  56.          *
  57.          * 内存映照文件绑定
  58.          * @return
  59.          */
  60.         public synchronized boolean boundChannelToByteBuffer() {
  61.                 try {
  62.                         RandomAccessFile raf = new RandomAccessFile(file, "rw");
  63.                         this.fileChannel = raf.getChannel();
  64.                 } catch (Exception e) {
  65.                         e.printStackTrace();
  66.                         this.boundSuccess = false;
  67.                         return false;
  68.                 }
  69.  
  70.                 try {
  71.                         this.mappedByteBuffer = this.fileChannel
  72.                                         .map(FileChannel.MapMode.READ_WRITE, 0, MAX_FILE_SIZE);
  73.                 } catch (IOException e) {
  74.                         e.printStackTrace();
  75.                         this.boundSuccess = false;
  76.                         return false;
  77.                 }
  78.  
  79.                 this.boundSuccess = true;
  80.                 return true;
  81.         }
  82.        
  83.         /**
  84.          * 写数据:先将之前的文件删除然后重新
  85.          * @param data
  86.          * @return
  87.          */
  88.         public synchronized boolean writeData(byte[] data) {
  89.                
  90.                 return false;
  91.         }
  92.        
  93.         /**
  94.          * 在文件末尾追加数据
  95.          * @param data
  96.          * @return
  97.          * @throws Exception
  98.          */
  99.         public synchronized boolean appendData(byte[] data) throws Exception {
  100.                 if (!boundSuccess) {
  101.                         boundChannelToByteBuffer();
  102.                 }
  103.                
  104.                 writePosition = writePosition + data.length;
  105.                 if (writePosition >= MAX_FILE_SIZE) {   // 如果写入data会超出文件大小限制,不写入
  106.                         flush();
  107.                         writePosition = writePosition - data.length;
  108.                         System.out.println("File="
  109.                                                                 + file.toURI().toString()
  110.                                                                 + " is written full.");
  111.                         System.out.println("already write data length:"
  112.                                                                 + writePosition
  113.                                                                 + ", max file size=" + MAX_FILE_SIZE);
  114.                         return false;
  115.                 }
  116.  
  117.                 this.mappedByteBuffer.put(data);
  118.  
  119.                 // 检查是否需要把内存缓冲刷到磁盘
  120.                 if ( (writePosition - lastFlushFilePosition > this.MAX_FLUSH_DATA_SIZE)
  121.                          ||
  122.                          (System.currentTimeMillis() - lastFlushTime > this.MAX_FLUSH_TIME_GAP
  123.                           && writePosition > lastFlushFilePosition) ) {
  124.                         flush();   // 刷到磁盘
  125.                 }
  126.                
  127.                 return true;
  128.         }
  129.  
  130.         public synchronized void flush() {
  131.                 this.mappedByteBuffer.force();
  132.                 this.lastFlushTime = System.currentTimeMillis();
  133.                 this.lastFlushFilePosition = writePosition;
  134.         }
  135.  
  136.         public long getLastFlushTime() {
  137.                 return lastFlushTime;
  138.         }
  139.  
  140.         public String getFileName() {
  141.                 return fileName;
  142.         }
  143.  
  144.         public String getFileDirPath() {
  145.                 return fileDirPath;
  146.         }
  147.  
  148.         public boolean isBundSuccess() {
  149.                 return boundSuccess;
  150.         }
  151.  
  152.         public File getFile() {
  153.                 return file;
  154.         }
  155.  
  156.         public static long getMaxFileSize() {
  157.                 return MAX_FILE_SIZE;
  158.         }
  159.  
  160.         public long getWritePosition() {
  161.                 return writePosition;
  162.         }
  163.  
  164.         public long getLastFlushFilePosition() {
  165.                 return lastFlushFilePosition;
  166.         }
  167.  
  168.         public long getMAX_FLUSH_DATA_SIZE() {
  169.                 return MAX_FLUSH_DATA_SIZE;
  170.         }
  171.  
  172.         public long getMAX_FLUSH_TIME_GAP() {
  173.                 return MAX_FLUSH_TIME_GAP;
  174.         }
  175.  
  176. }//源代码片段来自云代码http://yuncode.net
  177.                        

回复 "Java NIO在文件末尾追加数据"

这儿你可以回复上面这条便签

captcha